This commit is contained in:
Eric Mertens 2024-12-28 14:24:10 -06:00
parent 969215f3fa
commit 1b949772c2
2 changed files with 13 additions and 8 deletions

View File

@ -14,8 +14,9 @@ namespace {
auto suffixed(std::uint64_t x, std::uint64_t y) -> std::optional<std::uint64_t> auto suffixed(std::uint64_t x, std::uint64_t y) -> std::optional<std::uint64_t>
{ {
while (x > 0 && x % 10 != y % 10) { while (x > 0) {
if (y == 0) { return x; } if (y == 0) { return x; }
if (x % 10 != y % 10) return std::nullopt;
x /= 10; x /= 10;
y /= 10; y /= 10;
} }
@ -53,14 +54,16 @@ auto check(bool const part2, std::uint64_t const target, std::vector<std::uint64
} // namespace } // namespace
auto Main(std::istream & in, std::ostream & out) -> void auto Main(std::istream & in, std::ostream & out) -> void
{ {
std::vector<boost::tuple<std::uint64_t, std::vector<std::uint64_t>>> input; auto const input =
aocpp::ParseSimple(in, *(qi::ulong_long >> ':' >> *(' ' >> qi::ulong_long) >> '\n'), input); aocpp::ParseSimple
<std::vector<boost::tuple<std::uint64_t, std::vector<std::uint64_t>>>>
(in, *(qi::ulong_long >> ':' >> *(' ' >> qi::ulong_long) >> '\n'));
std::uint64_t p1{}, p2{}; std::uint64_t p1{}, p2{};
for (auto && [x, xs] : input) { for (auto && [x, xs] : input) {
if (check(false, x, xs)) p1 += x; if (check(false, x, xs)) p1 += x;
if (check(true, x, xs)) p2 += x; if (check(true, x, xs)) p2 += x;
} }
out << "Part 1: " << p1 << "\n" out << "Part 1: " << p1 << "\n"

View File

@ -46,9 +46,10 @@ auto ParseGrammar_(G const& grammar, std::istream & in) -> typename G::start_typ
return result; return result;
} }
template <typename P, typename R> template <typename R>
auto ParseSimple(std::istream& in, P p, R & result) -> void auto ParseSimple(std::istream& in, auto p) -> R
{ {
R result;
namespace qi = boost::spirit::qi; namespace qi = boost::spirit::qi;
auto const content = std::string{std::istreambuf_iterator{in}, {}}; auto const content = std::string{std::istreambuf_iterator{in}, {}};
qi::rule<std::string::const_iterator, R()> r { std::move(p) }; qi::rule<std::string::const_iterator, R()> r { std::move(p) };
@ -58,6 +59,7 @@ auto ParseSimple(std::istream& in, P p, R & result) -> void
if (!success || it != end) { if (!success || it != end) {
throw std::runtime_error("Parsing failed or input was not fully consumed."); throw std::runtime_error("Parsing failed or input was not fully consumed.");
} }
return result;
} }
} }