Day 2: Fulminating Farmland ★★

The train's first stop is a farm that harvests thunder from passing storms, which explains why the crates being loaded keep rumbling. Out in the fields, the Elves are tinkering with a farm robot that waters, fertilizes and weeds as it follows a long row of waypoints painted in a repeating rainbow: red, orange, yellow, green, blue, violet. The robot starts on red, and each color in its program sends it to the next waypoint of that color.

My input

Part A: run the program

Every move that steps backwards or lands on the same color wraps around a whole rainbow. Everything else cancels out, leaving the last color's number plus 6 for every wrap.

last (Y = 2) + 6 × wraps (652)
= 3,914

1,076 instructions in the program.

Part B: the Elves' trick

With broken keys, the Elves can only rearrange the program, always picking the farthest move. That groups the colors in R V B G Y O order: repeats are full laps of 6, and each change of group falls a little short. The total shortfall is how far back around the rainbow the last group (O) sits from red.

6 × n (1,076) − shortfall (5)
= 6,451

R×276 V×178 B×201 G×0 Y×143 O×278
✓ matches the walk

Try your own program

Submit a robot program (a string of R, O, Y, G, B and V) to see a walkthrough, one waypoint at a time.