7 January 2020 ; tagged in: advent of code , haskell

Advent of Code 2019 day 21

Solving by inspection

Advent of Code 2019 day 21

A large change of pace for day 21! I solved these by a bit of thought, and then some inspection of the test outputs.

Part 1

The idea here is "jump if you can see a hole, so long as there is ground at your landing space." That turns into the Boolean expression

\[ ( \lnot A \lor \lnot B \lor \lnot C ) \land D ) \]

which de Morgan's theorem turns into

\[ \lnot (A \land B \land C ) \land D \]

Given that the T and J registers start as False, that expression becomes the program

OR  A T
AND B T
AND C T
NOT T J
AND D J
WALK

Part 2

There were two cases where the program above fails:

.................    .................
.................    .................
..@..............    ....@............
#####.#.##.######    #####...###...###
   ABCDEFGH               ABCDEFGH 

In the first case, the Part 1 program jumps onto the first, small island and then can't jump to the second. I fix that by including H in the check, giving the expression

\[ \lnot (A \land B \land C ) \land H \land D \]

But that change prevents the robot jumping in the second example. I fix that by including E as well

\[ \lnot (A \land B \land C ) \land (E \lor H) \land D \]

That gets turned into code

OR  A T
AND B T
AND C T
NOT T J
OR  E T
OR  H T
AND T J
AND D J
RUN

The tricky part is how to store the value of E into the T register, when I don't know what it's been set to. But as the jump condition is a conjunction, the robot is only jumping if J is true at this point, meaning T is false. That means I can insert E into T with the OR instruction.

Code

The code, such as it is, is available (and on Github).