This commit is contained in:
Eric Mertens 2024-12-28 14:25:56 -06:00
parent 1b949772c2
commit 35e84c20bb

View File

@ -12,7 +12,7 @@ namespace qi = boost::spirit::qi;
namespace {
auto suffixed(std::uint64_t x, std::uint64_t y) -> std::optional<std::uint64_t>
auto drop_suffix(std::uint64_t x, std::uint64_t y) -> std::optional<std::uint64_t>
{
while (x > 0) {
if (y == 0) { return x; }
@ -23,7 +23,7 @@ auto suffixed(std::uint64_t x, std::uint64_t y) -> std::optional<std::uint64_t>
return std::nullopt;
}
auto check(bool const part2, std::uint64_t const target, std::vector<std::uint64_t> const& numbers) -> bool
auto calibrated(bool const part2, std::uint64_t const target, std::vector<std::uint64_t> const& numbers) -> bool
{
if (numbers.size() == 0) { return false; }
@ -41,7 +41,7 @@ auto check(bool const part2, std::uint64_t const target, std::vector<std::uint64
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)) {
if (auto const u = drop_suffix(t, x)) {
work.emplace(i - 1, *u);
}
}
@ -60,10 +60,10 @@ auto Main(std::istream & in, std::ostream & out) -> void
<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 = 0, p2 = 0;
for (auto && [x, xs] : input) {
if (check(false, x, xs)) p1 += x;
if (check(true, x, xs)) p2 += x;
if (calibrated(false, x, xs)) p1 += x;
if (calibrated(true, x, xs)) p2 += x;
}
out << "Part 1: " << p1 << "\n"