From 969215f3fa88cfd60f4c978b5e7c9a0e0660e804 Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Sat, 28 Dec 2024 14:00:33 -0600 Subject: [PATCH] fiddle --- 2024/07.cpp | 39 ++++++++++++++++++++--------------- lib/include/aocpp/Parsing.hpp | 1 - 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/2024/07.cpp b/2024/07.cpp index dfdd22e..a172007 100644 --- a/2024/07.cpp +++ b/2024/07.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -11,34 +12,38 @@ namespace qi = boost::spirit::qi; namespace { -auto -suffixed(std::uint64_t x, std::uint64_t y) -> std::optional +auto suffixed(std::uint64_t x, std::uint64_t y) -> std::optional { - while (x > 0) { + while (x > 0 && x % 10 != y % 10) { if (y == 0) { return x; } - if (x % 10 != y % 10) { return {}; } x /= 10; y /= 10; } - return {}; + return std::nullopt; } -auto -check(bool part2, std::uint64_t target, std::vector const& numbers) -> bool +auto check(bool const part2, std::uint64_t const target, std::vector const& numbers) -> bool { - std::stack> work; - work.push({numbers.size() - 1, target}); + if (numbers.size() == 0) { return false; } - while (!work.empty()){ - auto [i, t] = work.top(); + auto work = std::stack>{}; + work.emplace(numbers.size() - 1, target); + + while (not work.empty()) + { + auto const [i, t] = work.top(); work.pop(); - auto x = numbers[i]; + auto const x = numbers[i]; if (i == 0) { - if (x == t) return true; + if (x == t) { return true; } } else { - if (x < t) { work.push({i-1, t-x}); } - if (t % x == 0) { work.push({i-1, t/x}); } - if (part2) { if (auto u = suffixed(t,x)) { work.push({i-1, *u}); } } + if (x < t) { work.emplace(i - 1, t - x); } + if (t % x == 0) { work.emplace(i - 1, t / x); } + if (part2) { + if (auto const u = suffixed(t, x)) { + work.emplace(i - 1, *u); + } + } } } @@ -50,7 +55,7 @@ check(bool part2, std::uint64_t target, std::vector const& number auto Main(std::istream & in, std::ostream & out) -> void { std::vector>> input; - aocpp::ParseSimple(in, *(qi::ulong_long >> ":" >> *(" " >> qi::ulong_long) >> "\n"), input); + aocpp::ParseSimple(in, *(qi::ulong_long >> ':' >> *(' ' >> qi::ulong_long) >> '\n'), input); std::uint64_t p1{}, p2{}; for (auto && [x, xs] : input) { diff --git a/lib/include/aocpp/Parsing.hpp b/lib/include/aocpp/Parsing.hpp index a1c9f31..2976698 100644 --- a/lib/include/aocpp/Parsing.hpp +++ b/lib/include/aocpp/Parsing.hpp @@ -7,7 +7,6 @@ #include #include -#include namespace aocpp {