diff --git a/2024/07.cpp b/2024/07.cpp index a172007..83c1e74 100644 --- a/2024/07.cpp +++ b/2024/07.cpp @@ -14,8 +14,9 @@ namespace { auto suffixed(std::uint64_t x, std::uint64_t y) -> std::optional { - while (x > 0 && x % 10 != y % 10) { + while (x > 0) { if (y == 0) { return x; } + if (x % 10 != y % 10) return std::nullopt; x /= 10; y /= 10; } @@ -53,14 +54,16 @@ auto check(bool const part2, std::uint64_t const target, std::vector void -{ - std::vector>> input; - aocpp::ParseSimple(in, *(qi::ulong_long >> ':' >> *(' ' >> qi::ulong_long) >> '\n'), input); - +{ + auto const input = + aocpp::ParseSimple + >>> + (in, *(qi::ulong_long >> ':' >> *(' ' >> qi::ulong_long) >> '\n')); + std::uint64_t p1{}, p2{}; for (auto && [x, xs] : input) { 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" diff --git a/lib/include/aocpp/Parsing.hpp b/lib/include/aocpp/Parsing.hpp index 2976698..2683431 100644 --- a/lib/include/aocpp/Parsing.hpp +++ b/lib/include/aocpp/Parsing.hpp @@ -46,9 +46,10 @@ auto ParseGrammar_(G const& grammar, std::istream & in) -> typename G::start_typ return result; } -template -auto ParseSimple(std::istream& in, P p, R & result) -> void +template +auto ParseSimple(std::istream& in, auto p) -> R { + R result; namespace qi = boost::spirit::qi; auto const content = std::string{std::istreambuf_iterator{in}, {}}; qi::rule r { std::move(p) }; @@ -58,6 +59,7 @@ auto ParseSimple(std::istream& in, P p, R & result) -> void if (!success || it != end) { throw std::runtime_error("Parsing failed or input was not fully consumed."); } + return result; } }