--- Day 6: Guard Gallivant ---
The Historians use their fancy device again, this time to whisk you all away to the North Pole prototype suit manufacturing lab... in the year 1518! It turns out that having direct access to history is very convenient for a group of historians.
You still have to be careful of time paradoxes, and so it will be important to avoid anyone from 1518 while The Historians search for the Chief. Unfortunately, a single guard is patrolling this part of the lab.
Maybe you can work out where the guard will go ahead of time so that The Historians can search safely?
You start by making a map (your puzzle input) of the situation. For example:
....#..... .........# .......... ..#....... .......#.. .......... .#..^..... ........#. #......... ......#...
The map shows the current position of the guard with
^
(to indicate the guard is currently facing up from the perspective of the map). Any obstructions - crates, desks, alchemical reactors, etc. - are shown as#
.Lab guards in 1518 follow a very strict patrol protocol which involves repeatedly following these steps:
- If there is something directly in front of you, turn right 90 degrees.
- Otherwise, take a step forward.
Following the above protocol, the guard moves up several times until she reaches an obstacle (in this case, a pile of failed suit prototypes):
....#..... ....^....# .......... ..#....... .......#.. .......... .#........ ........#. #......... ......#...
Because there is now an obstacle in front of the guard, she turns right before continuing straight in her new facing direction:
....#..... ........># .......... ..#....... .......#.. .......... .#........ ........#. #......... ......#...
Reaching another obstacle (a spool of several very long polymers), she turns right again and continues downward:
....#..... .........# .......... ..#....... .......#.. .......... .#......v. ........#. #......... ......#...
This process continues for a while, but the guard eventually leaves the mapped area (after walking past a tank of universal solvent):
....#..... .........# .......... ..#....... .......#.. .......... .#........ ........#. #......... ......#v..
By predicting the guard's route, you can determine which specific positions in the lab will be in the patrol path. Including the guard's starting position, the positions visited by the guard before leaving the area are marked with an
X
:....#..... ....XXXXX# ....X...X. ..#.X...X. ..XXXXX#X. ..X.X.X.X. .#XXXXXXX. .XXXXXXX#. #XXXXXXX.. ......#X..
In this example, the guard will visit
41
distinct positions on your map.Predict the path of the guard. How many distinct positions will the guard visit before leaving the mapped area?
show start: first raze (til count g),/:'where each "^"=0N!g: read0 `6.txt ("....#.....";".........#";"..........";"..#.......";".......#..";"............ 6 4
We can now amend the grid to treat the starting space as an empty space, a dot.
g: .[g; start; :; "."]Also define the four directions, up, right, left, down. These will be represented with 0 to 3.
dirs: (-1 0;0 1;1 0;0 -1)We can now make a function to traverse the grid, which will be called with a direction and a position.
trav[dir;pos]
or trav . (dir;pos)
.
The first step is to calculate the new position.
new: y + dirs x
Now, if the new position is a dot, we return it. If it is a hash, instead we rotate 90 degrees and stay where we are. Otherwise, we have a null value from indexing out of bounds. In this case, we want to return the original value. This will make it easy to detect when we have at the edge of the grid.
trav: {$["."=c: g . new:y + dirs x; (x;new); "#"=c; (mod[x+1;4];y); (x;y)]}.The dot on the end is so it can take a 2 item list as an argument.
Now, we use scan to traverse from the start, until we leave the grid.
steps: trav scan (0;start)
Now, we can calculate the number of visited positions. We save the positions in a variable path for later use.
count path: distinct last each steps 41
We can also visualise the visited positions.
g .[;;:;"X"]/ path "....#....." "....XXXXX#" "....X...X." "..#.X...X." "..XXXXX#X." "..X.X.X.X." ".#XXXXXXX." ".XXXXXXX#." "#XXXXXXX.." "......#X.."
--- Part Two ---
While The Historians begin working around the guard's patrol route, you borrow their fancy device and step outside the lab. From the safety of a supply closet, you time travel through the last few months and record the nightly status of the lab's guard post on the walls of the closet.
Returning after what seems like only a few seconds to The Historians, they explain that the guard's patrol area is simply too large for them to safely search the lab without getting caught.
Fortunately, they are pretty sure that adding a single new obstruction won't cause a time paradox. They'd like to place the new obstruction in such a way that the guard will get stuck in a loop, making the rest of the lab safe to search.
To have the lowest chance of creating a time paradox, The Historians would like to know all of the possible positions for such an obstruction. The new obstruction can't be placed at the guard's starting position - the guard is there right now and would notice.
In the above example, there are only
6
different positions where a new obstruction would cause the guard to get stuck in a loop. The diagrams of these six situations useO
to mark the new obstruction,|
to show a position where the guard moves up/down,-
to show a position where the guard moves left/right, and+
to show a position where the guard moves both up/down and left/right.Option one, put a printing press next to the guard's starting position:
....#..... ....+---+# ....|...|. ..#.|...|. ....|..#|. ....|...|. .#.O^---+. ........#. #......... ......#...
Option two, put a stack of failed suit prototypes in the bottom right quadrant of the mapped area:
....#..... ....+---+# ....|...|. ..#.|...|. ..+-+-+#|. ..|.|.|.|. .#+-^-+-+. ......O.#. #......... ......#...
Option three, put a crate of chimney-squeeze prototype fabric next to the standing desk in the bottom right quadrant:
....#..... ....+---+# ....|...|. ..#.|...|. ..+-+-+#|. ..|.|.|.|. .#+-^-+-+. .+----+O#. #+----+... ......#...
Option four, put an alchemical retroencabulator near the bottom left corner:
....#..... ....+---+# ....|...|. ..#.|...|. ..+-+-+#|. ..|.|.|.|. .#+-^-+-+. ..|...|.#. #O+---+... ......#...
Option five, put the alchemical retroencabulator a bit to the right instead:
....#..... ....+---+# ....|...|. ..#.|...|. ..+-+-+#|. ..|.|.|.|. .#+-^-+-+. ....|.|.#. #..O+-+... ......#...
Option six, put a tank of sovereign glue right next to the tank of universal solvent:
....#..... ....+---+# ....|...|. ..#.|...|. ..+-+-+#|. ..|.|.|.|. .#+-^-+-+. .+----++#. #+----++.. ......#O..
It doesn't really matter what you choose to use as an obstacle so long as you and The Historians can put it into position without the guard noticing. The important thing is having enough options that you can find one that minimizes time paradoxes, and in this example, there are
6
different positions you could choose.You need to get the guard stuck in a loop by adding a single new obstruction. How many different positions could you choose for this obstruction?
The first observation is that we only need to consider adding an obstruction to a position that occurred in the path. This must be the case as adding one elsewhere wouldn't make any difference.
The second one is that now we don't care about the path length, only whether or not it loops. This means we can do something a bit better.
Let's write a function that jumps to the next # (or just before it), rather than traversing each step.
Firstly, we'll need to find the obstructions. We need lists of indices per rows and columns.
show obs: where each' 1 flip\ "#" = g ,4 ,9 `long$() ,2 ,7 `long$() ,1 ,8 ,0 ,6 ,8 ,6 ,3 `long$() ,0 `long$() ,9 ,4 ,7 ,1
Now, if we are going up or left, (directions 0 or 3) we want to find the previous index, and then add 1. However, if we are going down or right, we want to find the next index, and then subtract 1.
We can construct these two parts like this.
{b: 0110b x; (`prev`next b; 1 -1[b])} 0 1 2 3 prev next next prev 1 -1 -1 1
How do we find these? Finding the previous index can be done with bin, and the next one can be done with binr.
Actually, the next can be done with bin too, by adding 1 to the index. This returns an incorrect result if the value is contained in the list, but we know that can't happen (we will never be inside an obstruction).
The next step is to do the search. We pick which values we're looking at based on if we're going horizontally or vertically. This is equivalent to the direction mod 2. Let's test it with the start value, and heading up.
{b:0110b x;o:obs[1-c;y@1-c:x mod 2];y[c]:1 -1[b]+ o@b+o bin y c;((1+x)mod 4;y)}[0; start] 1 1 4
We can see (if running with sample input) that this returns the correct next position.
Now, we can save that as a function and use it to test for loops.
The last part is to have a way of inserting the value. There are different ways to do this, but the easiest is to make a new global version of obs, and edit the right lists.
ins: {W::obs; W[0;x]:asc W[0;x],y; W[1;y]:asc W[1;y],x}.
This function creates a new global called W, and inserts y into the relevant x list, and x into the relevant y list.
jump:{b:0110b x;o:W[1-c;y@1-c:x mod 2];y[c]:1 -1[b]+ o@b+o bin y c;((1+x)mod 4;y)}.
Now, to test for loops, we can use a trick. Rather than testing ourselves, we can let the interpreter do the work!
If we have a list with the unique attribute, and append something we've already seen, it will lose the attribute.
So testing if it is unique is equivalent to testing if it has the unique attribute.
The steps now are:
ins x
S:`u#enlist s:(0;start)
while[`u~attr S; S,:s:jump s]
"." = g . last s
Our jump function will return null if we have left the grid, so g . last s would be a space.
sum {...} each 1_path
The 1_
is so we don't insert the starting value.
sum {ins x; S:`u#enlist s:(0;start); while[`u~attr S;S,:s:jump s]; "." = g . last s} each 1_path
g:read0 `:i/6.txt
start: first raze (til count g) {x ,/: where y}' "^"=g
g: .[g;start;:;"."]
dirs: (-1 0;0 1;1 0;0 -1)
trav: {$["."=c: g . new:y + dirs x; (x;new); "#"=c; (mod[x+1;4];y); (x;y)]}.
count path: distinct last each steps: trav scan (0;start) / part 1
/ -1@g .[;;:;"X"]/ path; / visualise
obs: where each' 1 flip\ g = "#"
ins: {W::obs; W[0;x]:asc W[0;x],y; W[1;y]:asc W[1;y],x}.
jump:{b:0110b x;o:W[1-c;y@1-c:x mod 2];y[c]:1 -1[b]+ o@b+o bin y c;((1+x)mod 4;y)}.
sum {ins x; S:`u#enlist s:(0;start); while[`u~attr S;S,:s:jump s]; "."=g . last s} each 1_path
Pretty challenging problem, at least to do relatively efficiently, and it's only Day 6!
An obvious optimisation is not to start at the starting value each time, but start just before you reach that point in the path.
This simply requires replacing start
with steps@-1+path?x
Other than that, optimisaiton is difficult. As a rule of thumb, any each
should stick out as a potential slow point.
You get the most out of Q by using its built in vectorised operations. One possible way to "optimise" would be to rewrite the code so it wasn't reliant on globals, and replace each
with peach
. This doesn't really count though.
The true way to optimise this, is to try and find a way to process all possible insertions in parallel
However, this is hard, and the logic quickly gets confusing and difficult to follow. I'm not convinced the solution I ended up with is actually correct either, merely giving the right answer on my test data, which isn't quite the same.
That being said, here is my optimised solution, written in k. It runs under 100ms, which is comparable to solutions in compiled languages like Rust and C++. Not bad if I do say so myself.
s:*,/(!#g),/:'&:'"^"=g:0:`:i/6.txt;W:{-0W,(&x),0W}''1+:\"#"=g;d:3(|-1 1*)\-1 0
#u:? :/'({$[|/".^"=c:g/p:y+d x;(x;p);"#"=c;(1 2 3 0 x;y);(x;y)]}.)\(0;s)
X:{b:0110b x;v:w@'b+bin'[w:W[c;y c];L:y@~c:1010b x];x:1 2 3 0 x;
(x;@[y;~c;:;?[within[Z;(1-b)|:/(L;v)];Z:?[(E=y)c;E@~c;0N];v]+1-2*b])}.
+/&/~^*|200 X/0,,(#*E:+1_u)#'s