diff options
author | kaiwu <kaiwu2004@gmail.com> | 2023-01-25 11:34:28 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2023-01-25 11:34:28 +0800 |
commit | f08f78b465ab805f606bbde647a12e7b3a82750d (patch) | |
tree | ac0e51ef5a5d221409b8bcc0b52412dac4b37d7f | |
parent | 127fb396046cf60049d7ccf92212de5e9b6d3e36 (diff) | |
download | advent-of-code-f08f78b465ab805f606bbde647a12e7b3a82750d.tar.gz advent-of-code-f08f78b465ab805f606bbde647a12e7b3a82750d.zip |
2016 day15 done
-rw-r--r-- | src/2016/day15/README.md | 8 | ||||
-rw-r--r-- | src/2016/day15/aoc.cpp | 35 | ||||
-rw-r--r-- | test/test_2016.cpp | 4 |
3 files changed, 44 insertions, 3 deletions
diff --git a/src/2016/day15/README.md b/src/2016/day15/README.md index 4de57e7..c3d6cf4 100644 --- a/src/2016/day15/README.md +++ b/src/2016/day15/README.md @@ -23,3 +23,11 @@ If, however, you wait until time=5 to push the button, then when the capsule rea However, your situation has more than two discs; you've noted their positions in your puzzle input. What is the first time you can press the button to get a capsule? +--- Part Two --- + +After getting the first capsule (it contained a star! what great fortune!), the machine detects your success and begins to rearrange itself. + +When it's done, the discs are back in their original configuration as if it were time=0 again, but a new disc with 11 positions and starting at position 0 has appeared exactly one second below the previously-bottom disc. + +With this new disc, and counting again starting from time=0 with the configuration in your puzzle input, what is the first time you can press the button to get another capsule? + diff --git a/src/2016/day15/aoc.cpp b/src/2016/day15/aoc.cpp index e188291..318b345 100644 --- a/src/2016/day15/aoc.cpp +++ b/src/2016/day15/aoc.cpp @@ -2,5 +2,38 @@ namespace aoc2016 { -std::pair<int64_t, int64_t> day15(line_view) { return {0, 0}; } +size_t position_at(int t, int slot, size_t p) { return t > 0 ? (t % slot + p) % slot : p; } + +struct disc { + int slots; + size_t p; +}; + +static bool same(size_t* ps, int x) { + for (int i = 0; i < x; i++) { + if (ps[i] != ps[x]) { + return false; + } + } + return true; +} + +int first_catch(disc ds[7], int x) { + int t{0}; + while (true) { + size_t ps[7] = {0, 0, 0, 0, 0, 0, 0}; + for (size_t i = 0; i < 7; i++) { + ps[i] = position_at(t + i + 1, ds[i].slots, ds[i].p); + } + if (same(ps, x)) { + return t; + } + t++; + } +} + +std::pair<int64_t, int64_t> day15(line_view) { + disc ds[7] = {{17, 1}, {7, 0}, {19, 2}, {5, 0}, {3, 0}, {13, 5}, {11, 0}}; + return {first_catch(ds, 5), first_catch(ds, 6)}; +} } // namespace aoc2016 diff --git a/test/test_2016.cpp b/test/test_2016.cpp index 19e6ad0..658acce 100644 --- a/test/test_2016.cpp +++ b/test/test_2016.cpp @@ -141,8 +141,8 @@ TEST_CASE("One-Time Pad", "[2016]") { TEST_CASE("Timing is Everything", "[2016]") { line_view lv = load_file("../src/2016/day15/input"); auto p = aoc2016::day15(lv); - REQUIRE(0 == p.first); - REQUIRE(0 == p.second); + REQUIRE(317371 == p.first); + REQUIRE(2080951 == p.second); } |