#include #include #include #include #include #include #include #include namespace { struct Scanner { std::uint64_t depth, range, position, length; }; auto Parse(std::istream & in) -> std::vector { std::uint64_t depth, range; char skip; std::vector result; while (in >> depth >> skip >> range) { auto len = range*2-2; result.push_back(Scanner{depth, range, depth%len, len}); } return result; } auto Caught(std::vector const& scanners) -> std::size_t { std::uint64_t severity = 0; for (auto const& scanner : scanners) { if (scanner.position == 0) { severity += scanner.depth * scanner.range; } } return severity; } auto Delay(std::vector scanners) { std::uint64_t delay = 0; for (;;) { bool success = true; for (auto & scanner : scanners) { if (scanner.position++ == 0) { success = false; } if (scanner.position == scanner.length) scanner.position = 0; } if (success) break; delay++; } return delay; } } // namespace TEST_SUITE("2017-12 examples") { TEST_CASE("example") { std::istringstream in { "0: 3\n" "1: 2\n" "4: 4\n" "6: 4\n" }; auto input = Parse(in); CHECK(Caught(input) == 24); CHECK(Delay(input) == 10); } } auto Main(std::istream & in, std::ostream & out) -> void { auto input {Parse(in)}; out << "Part 1: " << Caught(input) << std::endl; out << "Part 2: " << Delay(std::move(input)) << std::endl; }