This commit is contained in:
Eric Mertens 2024-12-28 14:00:33 -06:00
parent b18f6ae092
commit 969215f3fa
2 changed files with 22 additions and 18 deletions

View File

@ -3,6 +3,7 @@
#include <boost/tuple/tuple.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/boost_tuple.hpp>
#include <optional>
#include <stack>
@ -11,34 +12,38 @@ namespace qi = boost::spirit::qi;
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) {
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<std::uint64_t> const& numbers) -> bool
auto check(bool const part2, std::uint64_t const target, std::vector<std::uint64_t> const& numbers) -> bool
{
std::stack<boost::tuple<std::size_t, std::uint64_t>> 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<boost::tuple<std::size_t, std::uint64_t>>{};
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<std::uint64_t> const& number
auto Main(std::istream & in, std::ostream & out) -> void
{
std::vector<boost::tuple<std::uint64_t, std::vector<std::uint64_t>>> 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) {

View File

@ -7,7 +7,6 @@
#include <vector>
#include <boost/spirit/home/qi.hpp>
#include <boost/fusion/include/boost_tuple.hpp>
namespace aocpp {