From f021fddf3501d5cbb301421fb6dfcf84b6b921ee Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Fri, 25 Nov 2022 12:07:41 -0800 Subject: [PATCH] 2017-13 --- 2017/13.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++++ 2017/CMakeLists.txt | 3 ++ 2 files changed, 82 insertions(+) create mode 100644 2017/13.cpp diff --git a/2017/13.cpp b/2017/13.cpp new file mode 100644 index 0000000..682d578 --- /dev/null +++ b/2017/13.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include +#include + +#include + +#include + +using namespace aocpp; + +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(int argc, char** argv) -> int { + auto input = Parse(*Startup(argc, argv)); + std::cout << "Part 1: " << Caught(input) << std::endl; + std::cout << "Part 2: " << Delay(input) << std::endl; +} \ No newline at end of file diff --git a/2017/CMakeLists.txt b/2017/CMakeLists.txt index cbd2fb4..9271f54 100644 --- a/2017/CMakeLists.txt +++ b/2017/CMakeLists.txt @@ -13,6 +13,9 @@ target_link_libraries(2017_11 aocpp) add_executable(2017_12 12.cpp) target_link_libraries(2017_12 aocpp) +add_executable(2017_13 13.cpp) +target_link_libraries(2017_13 aocpp) + add_executable(2017_14 14.cpp) target_link_libraries(2017_14 aocpp knothash)