diff options
author | kaiwu <kaiwu2004@gmail.com> | 2023-02-09 10:06:45 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2023-02-09 10:06:45 +0800 |
commit | d7f2eb88d8d17e91657b03be6376761338cd6141 (patch) | |
tree | a9376a546fc6a3a15962ca741d0e4d9435ec3056 | |
parent | 204146e91d1ff8b625c9c91a5a93e85c8408c801 (diff) | |
download | advent-of-code-d7f2eb88d8d17e91657b03be6376761338cd6141.tar.gz advent-of-code-d7f2eb88d8d17e91657b03be6376761338cd6141.zip |
2017 day13 part1
-rw-r--r-- | src/2017/day13/README.md | 113 | ||||
-rw-r--r-- | src/2017/day13/aoc.cpp | 10 | ||||
-rw-r--r-- | src/2017/day13/aoc.h | 7 | ||||
-rw-r--r-- | test/test_2017.cpp | 2 |
4 files changed, 126 insertions, 6 deletions
diff --git a/src/2017/day13/README.md b/src/2017/day13/README.md index 62c6429..ed14cad 100644 --- a/src/2017/day13/README.md +++ b/src/2017/day13/README.md @@ -157,3 +157,116 @@ The severity of getting caught on a layer is equal to its depth multiplied by it Given the details of the firewall you've recorded, if you leave immediately, what is the severity of your whole trip? +--- Part Two --- +Now, you need to pass through the firewall without being caught - easier said than done. + +You can't control the speed of the packet, but you can delay it any number of picoseconds. For each picosecond you delay the packet before beginning your trip, all security scanners move one step. You're not in the firewall during this time; you don't enter layer 0 until you stop delaying the packet. + +In the example above, if you delay 10 picoseconds (picoseconds 0 - 9), you won't get caught: + +State after delaying: + 0 1 2 3 4 5 6 +[ ] [S] ... ... [ ] ... [ ] +[ ] [ ] [ ] [ ] +[S] [S] [S] + [ ] [ ] + +Picosecond 10: + 0 1 2 3 4 5 6 +( ) [S] ... ... [ ] ... [ ] +[ ] [ ] [ ] [ ] +[S] [S] [S] + [ ] [ ] + + 0 1 2 3 4 5 6 +( ) [ ] ... ... [ ] ... [ ] +[S] [S] [S] [S] +[ ] [ ] [ ] + [ ] [ ] + + +Picosecond 11: + 0 1 2 3 4 5 6 +[ ] ( ) ... ... [ ] ... [ ] +[S] [S] [S] [S] +[ ] [ ] [ ] + [ ] [ ] + + 0 1 2 3 4 5 6 +[S] (S) ... ... [S] ... [S] +[ ] [ ] [ ] [ ] +[ ] [ ] [ ] + [ ] [ ] + + +Picosecond 12: + 0 1 2 3 4 5 6 +[S] [S] (.) ... [S] ... [S] +[ ] [ ] [ ] [ ] +[ ] [ ] [ ] + [ ] [ ] + + 0 1 2 3 4 5 6 +[ ] [ ] (.) ... [ ] ... [ ] +[S] [S] [S] [S] +[ ] [ ] [ ] + [ ] [ ] + + +Picosecond 13: + 0 1 2 3 4 5 6 +[ ] [ ] ... (.) [ ] ... [ ] +[S] [S] [S] [S] +[ ] [ ] [ ] + [ ] [ ] + + 0 1 2 3 4 5 6 +[ ] [S] ... (.) [ ] ... [ ] +[ ] [ ] [ ] [ ] +[S] [S] [S] + [ ] [ ] + + +Picosecond 14: + 0 1 2 3 4 5 6 +[ ] [S] ... ... ( ) ... [ ] +[ ] [ ] [ ] [ ] +[S] [S] [S] + [ ] [ ] + + 0 1 2 3 4 5 6 +[ ] [ ] ... ... ( ) ... [ ] +[S] [S] [ ] [ ] +[ ] [ ] [ ] + [S] [S] + + +Picosecond 15: + 0 1 2 3 4 5 6 +[ ] [ ] ... ... [ ] (.) [ ] +[S] [S] [ ] [ ] +[ ] [ ] [ ] + [S] [S] + + 0 1 2 3 4 5 6 +[S] [S] ... ... [ ] (.) [ ] +[ ] [ ] [ ] [ ] +[ ] [S] [S] + [ ] [ ] + + +Picosecond 16: + 0 1 2 3 4 5 6 +[S] [S] ... ... [ ] ... ( ) +[ ] [ ] [ ] [ ] +[ ] [S] [S] + [ ] [ ] + + 0 1 2 3 4 5 6 +[ ] [ ] ... ... [ ] ... ( ) +[S] [S] [S] [S] +[ ] [ ] [ ] + [ ] [ ] +Because all smaller delays would get you caught, the fewest number of picoseconds you would need to delay to get through safely is 10. + +What is the fewest number of picoseconds that you need to delay the packet to pass through the firewall without being caught? diff --git a/src/2017/day13/aoc.cpp b/src/2017/day13/aoc.cpp index e3cf129..bab6825 100644 --- a/src/2017/day13/aoc.cpp +++ b/src/2017/day13/aoc.cpp @@ -9,9 +9,17 @@ std::pair<int64_t, int64_t> day13(line_view file) { return true; }); + int severity{0}; + for (auto& s : vs) { + if (s.at_level(s.depth) == 0) { + // s.print(); + severity += s.depth * s.range; + } + } + // for (auto& s: vs) { // s.print(); // } - return {0, 0}; + return {severity, 0}; } } // namespace aoc2017 diff --git a/src/2017/day13/aoc.h b/src/2017/day13/aoc.h index 5e46b4f..fabc93d 100644 --- a/src/2017/day13/aoc.h +++ b/src/2017/day13/aoc.h @@ -17,12 +17,11 @@ struct scanner { *pp = p; } - void print() const noexcept { - printf("%d %d\n", depth, range); - } + void print() const noexcept { printf("%d %d\n", depth, range); } int at_level(int t) const noexcept { - return 0; + t %= 2 * (range - 1); + return t < range ? t : 2 * (range - 1) - t; } scanner(line_view lv) { diff --git a/test/test_2017.cpp b/test/test_2017.cpp index 57777b8..db8d5b9 100644 --- a/test/test_2017.cpp +++ b/test/test_2017.cpp @@ -141,7 +141,7 @@ TEST_CASE("Digital Plumber", "[2017]") { TEST_CASE("Packet Scanners", "[2017]") { line_view lv = load_file("../src/2017/day13/input"); auto p = aoc2017::day13(lv); - REQUIRE(0 == p.first); + REQUIRE(2384 == p.first); REQUIRE(0 == p.second); } |