Part 1
Description
A room is made of rows of tiles, each either safe (.) or a trap (^). The first row is my puzzle input. Every other row is worked out from the row above: a tile is a trap if its left, centre and right neighbours above are (trap, trap, safe), (safe, trap, trap), (trap, safe, safe) or (safe, safe, trap), and it's safe otherwise. Beyond the two ends of a row, the tiles count as safe. Part 1 asks for the number of safe tiles in the first 40 rows.
Solution
I put my input in A4, and cut it into single tiles in the first row of the grid:
L7: =MID(A4,SEQUENCE(,LEN(A4)),1) ← fills L7:DG7 (100 tiles)
Every tile below that looks at the three tiles above it. This is the formula for L8, written out with the four rules from the puzzle, and I filled it right and down to row 46 (40 rows in all):
L8: =IF((K7="^")<>(M7="^"),"^",".")
The two edges are taken care of by the columns next to the grid. Column K holds the row numbers 1 to 40 (K7: =SEQUENCE(40)), and a number is not a ^, so it counts as a safe tile on the left. The empty column after the last tile is the safe tile on the right. The answer is the number of safe tiles in the whole grid:
B2: =COUNTIF(L7:DG46,".")
The puzzle's small example (.^^.^.^^^^ for 10 rows) gives the 38 safe tiles from the puzzle text.
Part 2
Description
Now the room has 400,000 rows, and Part 2 asks for the safe tiles again.
Solution
Drawing 400,000 rows of 100 tiles would take 40 million cells, so I only keep the row I am working on. First, look at the four rules again: the middle tile never matters. A tile is a trap exactly when its left and right neighbours are different. If I write safe as 1 and a trap as 0, then a new tile is 1 when the two neighbours are equal, and 0 when they differ. At the ends of the row, the missing neighbour is safe (1).
The formula keeps one row of numbers: the first number is the running total of safe tiles, and the 100 after it are the current row. REDUCE repeats the step 399,999 times (the first row, plus 399,999 more, makes 400,000):
D2: =LET(
a, SUBSTITUTE(SUBSTITUTE(A4,".",1),"^",0),
b, --MID(a,SEQUENCE(,LEN(a)),1),
s, SUM(b),
c, REDUCE(HSTACK(s,b),SEQUENCE(399999),
LAMBDA(p,q,
LET(
d, SEQUENCE(,LEN(a)),
e, IF(d=1,INDEX(TAKE(p,-1,-LEN(a)),,d+1),
IF(d=LEN(a),INDEX(TAKE(p,-1,-LEN(a)),,d-1),
IF(INDEX(TAKE(p,-1,-LEN(a)),,d-1)<>INDEX(TAKE(p,-1,-LEN(a)),,d+1),0,1)
)),
f, TAKE(p,-1,1)+SUM(e),
g,HSTACK(f,e),
g
)
)
),
TAKE(c,,1)
)
aturns the input into 1s (safe) and 0s (traps),bis the first row as numbers, andsis how many safe tiles it has.TAKE(p,-1,-LEN(a))is the current row (the last 100 numbers), andeis the next row. The first tile equals its right neighbour, because its left neighbour is the safe edge. The last tile equals its left neighbour. Every other tile is 0 if its two neighbours differ and 1 if they are equal.fadds the safe tiles of the new row to the running total, andHSTACK(f,e)is the new state.- At the end,
TAKE(c,,1)is the running total.
With only 39 repeats instead of 399,999 the same formula gives the answer to Part 1.


