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>
{
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;
}
@ -54,13 +55,15 @@ auto check(bool const part2, std::uint64_t const target, std::vector<std::uint64
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);
auto const 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{};
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"

View File

@ -46,9 +46,10 @@ auto ParseGrammar_(G const& grammar, std::istream & in) -> typename G::start_typ
return result;
}
template <typename P, typename R>
auto ParseSimple(std::istream& in, P p, R & result) -> void
template <typename R>
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<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) {
throw std::runtime_error("Parsing failed or input was not fully consumed.");
}
return result;
}
}