We're given a description of a firewall with "layers" and "security scanners". Each layer is a 1d grid with a different depth, and each scanner is bouncing up and down that layer as time passes. For part 1, we need to determine at what steps a packet gliding through the top layers gets "caught" by a scanner. For part2, we need to find the shortest amount of delay to wait before we can send a packet through without being caught.
This problem took an approach that's becoming increasingly familiar to me for Advent of Code problems. Describe a process, iterated in steps that can be simulated in data structures easily for part 1. Then, for part 2, make the numbers so large that it won't run in a reasonable amount of time. You'll either need to optimize or to switch to a new, math based approach instead of simulation based.
I wrote a simulation, but couldn't get past part 2 without adding the following basic optimizations:
- When calculating layers after delays, remember the previous layers instead of starting from scratch. If we are to try the layers after a 1,000 nanosecond delay, remember what they looked like after a 999 delay and increment once. (Don't start from 0 and step 1,000 times).
- As soon as you detect a packet is caught by a scanner, stop simulating. What happens in the future doesn't matter, since that delay is no longer a possible answer.
This got me the answer in about 2 minutes of runtime.
Then, I was able to remove layers of iterations by calculating the position of each scanner as I would arrive to it by using modulo math. This got me down to 10 seconds runtime. Not bad...
2017 Day 13 on AdventOfCode.com
You need to cross a vast firewall. The firewall consists of several layers, each with a security scanner that moves back and forth across the layer. To succeed, you must not be detected by a scanner.
By studying the firewall briefly, you are able to record (in your puzzle input) the depth of each layer and the range of the scanning area for the scanner within it, written as depth: range. Each layer has a thickness of exactly 1. A layer at depth 0 begins immediately inside the firewall; a layer at depth 1 would start immediately after that.
For example, suppose you've recorded the following:
0: 3
1: 2
4: 4
6: 4
This means that there is a layer immediately inside the firewall (with range 3), a second layer immediately after that (with range 2), a third layer which begins at depth 4 (with range 4), and a fourth layer which begins at depth 6 (also with range 4). Visually, it might look like this:
0 1 2 3 4 5 6
[ ] [ ] ... ... [ ] ... [ ]
[ ] [ ] [ ] [ ]
[ ] [ ] [ ]
[ ] [ ]
Within each layer, a security scanner moves back and forth within its range. Each security scanner starts at the top and moves down until it reaches the bottom, then moves up until it reaches the top, and repeats. A security scanner takes one picosecond to move one step. Drawing scanners as S, the first few picoseconds look like this:
Picosecond 0:
0 1 2 3 4 5 6
[S] [S] ... ... [S] ... [S]
[ ] [ ] [ ] [ ]
[ ] [ ] [ ]
[ ] [ ]
Picosecond 1:
0 1 2 3 4 5 6
[ ] [ ] ... ... [ ] ... [ ]
[S] [S] [S] [S]
[ ] [ ] [ ]
[ ] [ ]
Picosecond 2:
0 1 2 3 4 5 6
[ ] [S] ... ... [ ] ... [ ]
[ ] [ ] [ ] [ ]
[S] [S] [S]
[ ] [ ]
Picosecond 3:
0 1 2 3 4 5 6
[ ] [ ] ... ... [ ] ... [ ]
[S] [S] [ ] [ ]
[ ] [ ] [ ]
[S] [S]
Your plan is to hitch a ride on a packet about to move through the firewall. The packet will travel along the top of each layer, and it moves at one layer per picosecond. Each picosecond, the packet moves one layer forward (its first move takes it into layer 0), and then the scanners move one step. If there is a scanner at the top of the layer as your packet enters it, you are caught. (If a scanner moves into the top of its layer while you are there, you are not caught: it doesn't have time to notice you before you leave.) If you were to do this in the configuration above, marking your current position with parentheses, your passage through the firewall would look like this:
Initial state:
0 1 2 3 4 5 6
[S] [S] ... ... [S] ... [S]
[ ] [ ] [ ] [ ]
[ ] [ ] [ ]
[ ] [ ]
Picosecond 0:
0 1 2 3 4 5 6
(S) [S] ... ... [S] ... [S]
[ ] [ ] [ ] [ ]
[ ] [ ] [ ]
[ ] [ ]
0 1 2 3 4 5 6
( ) [ ] ... ... [ ] ... [ ]
[S] [S] [S] [S]
[ ] [ ] [ ]
[ ] [ ]
Picosecond 1:
0 1 2 3 4 5 6
[ ] ( ) ... ... [ ] ... [ ]
[S] [S] [S] [S]
[ ] [ ] [ ]
[ ] [ ]
0 1 2 3 4 5 6
[ ] (S) ... ... [ ] ... [ ]
[ ] [ ] [ ] [ ]
[S] [S] [S]
[ ] [ ]
Picosecond 2:
0 1 2 3 4 5 6
[ ] [S] (.) ... [ ] ... [ ]
[ ] [ ] [ ] [ ]
[S] [S] [S]
[ ] [ ]
0 1 2 3 4 5 6
[ ] [ ] (.) ... [ ] ... [ ]
[S] [S] [ ] [ ]
[ ] [ ] [ ]
[S] [S]
Picosecond 3:
0 1 2 3 4 5 6
[ ] [ ] ... (.) [ ] ... [ ]
[S] [S] [ ] [ ]
[ ] [ ] [ ]
[S] [S]
0 1 2 3 4 5 6
[S] [S] ... (.) [ ] ... [ ]
[ ] [ ] [ ] [ ]
[ ] [S] [S]
[ ] [ ]
Picosecond 4:
0 1 2 3 4 5 6
[S] [S] ... ... ( ) ... [ ]
[ ] [ ] [ ] [ ]
[ ] [S] [S]
[ ] [ ]
0 1 2 3 4 5 6
[ ] [ ] ... ... ( ) ... [ ]
[S] [S] [S] [S]
[ ] [ ] [ ]
[ ] [ ]
Picosecond 5:
0 1 2 3 4 5 6
[ ] [ ] ... ... [ ] (.) [ ]
[S] [S] [S] [S]
[ ] [ ] [ ]
[ ] [ ]
0 1 2 3 4 5 6
[ ] [S] ... ... [S] (.) [S]
[ ] [ ] [ ] [ ]
[S] [ ] [ ]
[ ] [ ]
Picosecond 6:
0 1 2 3 4 5 6
[ ] [S] ... ... [S] ... (S)
[ ] [ ] [ ] [ ]
[S] [ ] [ ]
[ ] [ ]
0 1 2 3 4 5 6
[ ] [ ] ... ... [ ] ... ( )
[S] [S] [S] [S]
[ ] [ ] [ ]
[ ] [ ]
In this situation, you are caught in layers 0 and 6, because your packet entered the layer when its scanner was at the top when you entered it. You are not caught in layer 1, since the scanner moved into the top of the layer once you were already there.
The severity of getting caught on a layer is equal to its depth multiplied by its range. (Ignore layers in which you do not get caught.) The severity of the whole trip is the sum of these values. In the example above, the trip severity is 03 + 64 = 24.
Given the details of the firewall you've recorded, if you leave immediately, what is the severity of your whole trip?
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?