From f4d56124832d5960fb9a6a89e38764fd49d066d9 Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Fri, 25 Nov 2022 15:27:43 -0800 Subject: [PATCH] 2017-17 --- 2017/17.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++ 2017/CMakeLists.txt | 3 ++ 2 files changed, 70 insertions(+) create mode 100644 2017/17.cpp diff --git a/2017/17.cpp b/2017/17.cpp new file mode 100644 index 0000000..bab4834 --- /dev/null +++ b/2017/17.cpp @@ -0,0 +1,67 @@ +#include +#include +#include + +#include + +#include + +using namespace aocpp; + +namespace { + +auto Step(std::vector & spin, int steps) -> void { + std::uint32_t cursor = spin.size() - 1; + for (int i = 0; i < steps; i++) { + cursor = spin[cursor]; + } + spin.push_back(spin[cursor]); + spin[cursor] = spin.size() - 1; + cursor = spin.size() - 1; +} + +auto Part1(std::uint64_t jump, std::uint64_t iterations) -> std::uint64_t { + std::vector spin {0}; + for (int j = 0; j < iterations; j++) { + Step(spin, jump); + } + return spin.back(); +} + +auto Part2(std::uint64_t jump, std::uint64_t iterations) -> std::uint64_t { + std::uint64_t cursor = 0; + std::uint64_t after_zero = -1; + for (std::uint64_t i = 1; i <= iterations; ++i) { + cursor += jump; + cursor %= i; + if (cursor == 0) { + after_zero = i; + } + cursor++; + } + + return after_zero; +} + +} // namespace + +TEST_SUITE("2017-17 examples") { + TEST_CASE("example") { + std::vector spin {0}; + Step(spin, 3); + REQUIRE(spin == std::vector{1,0}); + Step(spin, 3); + REQUIRE(spin == std::vector{2,0,1}); + Step(spin, 3); + REQUIRE(spin == std::vector{2,0,3,1}); + + CHECK(Part1(3, 2017) == 638); + } +} + +auto main(int argc, char** argv) -> int { + int steps; + *Startup(argc, argv) >> steps; + std::cout << "Part 1: " << Part1(steps, 2017) << std::endl; + std::cout << "Part 2: " << Part2(steps, 50'000'000) << std::endl; +} \ No newline at end of file diff --git a/2017/CMakeLists.txt b/2017/CMakeLists.txt index 9271f54..46a94b4 100644 --- a/2017/CMakeLists.txt +++ b/2017/CMakeLists.txt @@ -19,5 +19,8 @@ target_link_libraries(2017_13 aocpp) add_executable(2017_14 14.cpp) target_link_libraries(2017_14 aocpp knothash) +add_executable(2017_17 17.cpp) +target_link_libraries(2017_17 aocpp) + add_executable(2017_18 18.cpp) target_link_libraries(2017_18 aocpp)