Advent of Code is an annual challenge in which a new programming puzzle is released every day for the first 12 days of December. It’s an opportunity to solve problems, and we always see some great solutions in Dyalog APL. In this post I’m going to look at some ways to solve yesterday’s problem. Spoilers ahead!
Part 1
Yesterday’s problem asks us to imagine a series of differently-coloured waypoints in a field. These waypoints are red, orange, yellow, green, blue, and violet. They’re arranged in a repeating pattern in the field like this:
roygbvroygbvroygbvroy..
└────┘└────┘└────┘└──
We’re given a sequence of waypoint colours for a robot to visit, without backtracking and without visiting the same waypoint twice. The robot starts on the initial r, so the sequence obbvb means the robot takes this path:
┌→─┐ ┌┐
roygbvroygbvroygbv..
└┘ └──→──┘└─→──┘
└── 16 steps ───┘
In this example, the robot travels 16 steps. How can I write an APL program to work this out? For now, I’ll take x←'obbvb' so that I can visualise what’s going on. Later, I can load the full input from a file.
My first step was to prepend the inital red waypoint – the robot’s starting position:
x←'obbvb'
x←'r',x
I started by making the input numeric, so I can take the difference between elements to find how far apart they are:
Convert r o y g b v
to 0 1 2 3 4 5
I did this by looking up the input in 'roygbv' like this:
⎕IO←0
i←'roygbv'⍳x
i
0 1 4 4 5 4
⍝ x: r o b b v b
⍝ i: 0 1 4 4 5 4
I’m using ⎕IO←0 so that r corresponds to 0. Now I can find the size of the jumps from one waypoint to the next by taking pairwise differences:
¯2-/i
1 3 0 1 ¯1
⍝ r o b b v b
⍝ 0 1 4 4 5 4
⍝ 1 3 0 1 ¯1
This is close to what I need: I have the positive jumps from r to o, o to b, and b to v. There are two things to fix, though. I need the jump from the first b to the next to be a whole 6, but currently it’s 0, since we don’t stay in place. I also need the jump from the v to the next b to be 5, not ¯1, since we’re not supposed to backtrack.
I can take advantage of the repeating pattern to fix the negative numbers. Imagine the colours arranged in a circle:
r
v o
b y
g
Currently, the distance from v to b is ¯1, which can be thought of as stepping one place anticlockwise around this circle. We can also get from v to b by stepping 5 places clockwise. I can normalise our negative distances to positive ones by taking the residue mod 6, which maps any number of steps around this circle to the equivalent number of steps from 0 1 2 3 4 5:
6|¯2-/i
1 3 0 1 5
⍝ r o b b v b
⍝ 0 1 4 4 5 4
⍝ 1 3 0 1 5
I still need to change the 0 (the distance from b to itself) to a 6 (the distance from a b to the next b). An easy way to do this is to add 6 at the places that currently contain a 0:
+d←6|¯2-/i
1 3 0 1 5
6×0=d
0 0 6 0 0
d+←6×0=d
d
1 3 6 1 5
Now that I have the correct distances from each waypoint on the robot’s path to the next, I just need to sum them for the total distance. Putting it all together:
⎕IO←0
x←'obbvb'
x←'r',x ⍝ start at 'r'
i←'roygbv'⍳x ⍝ make into numbers
d←6|¯2-/i ⍝ distances, non-negative
d+←6×0=d ⍝ same colour → jump to next block
+/d ⍝ total distance travelled
16
You might prefer to present this as a one-liner:
x←'obbvb'
+/d+6×0=d←6|¯2-/'roygbv'⍳'r',x
16
This way, the name i is not needed. If you like tacit programming, you can also avoid using the name d to get a solution that uses no extra names:
x←'obbvb'
+/(⊢+6×0=⊢)6|¯2-/'roygbv'⍳'r',x
16
So far, I’ve been using a small example input. I can save the real input saved in a file called input.txt, and load it:
x←¯1↓⊃⎕NGET 'input.txt'
⎕NGET will load the file, ensuring that there is a trail line feed character. I don’t need that, so I drop it from the end.
Part 2
The second part of yesterday’s problem asks how far the robot would travel if we rearranged its target waypoints, one at a time, so that each jump is as far as possible. For instance, if the input was rybryb, we would re-arrange it to rrbbyy. This is because the first 2 jumps (initial r to r, then another r to r) are as long as possible (6 each). The next jump (r to b, length 5), is as long as we can make it with what we have left, and so on.
→ r → r → b → b → y → y (rrbbyy)
┌──→──┐ ┌──→──┐ ┌──→──┐
roygbvroygbvroygbvroygbvroygbvroygbv..
└──→──┘ └─→─┘ └─→─┘
└────────── 32 steps ───────────┘
One way to solve this problem is to re-arrange the input explicitly, and then do the same distance calculation that I did in part 1.
To do that, notice that when we can jump from a waypoint to another waypoint with the same colour, we’re maximising the size of the jump. So we know that colours must be grouped together, always taking the opportunity to jump to a waypoint with the same colour – we already saw that above, where the best arrangement resulted from grouping the colours together (rrbbyy).
Now that I know that the colours are grouped, I need to know what order the groups should be in. Since I’m implicitly starting on an r waypoint, the first group has to be all the rs. I want each jump between groups to be as long as possible, so it should be to the latest-occuring colour that hasn’t been used up yet. That is, I want to start with r, then visit the colours in the reverse order they appear in roygbv. So, the order is r→v→b→g→y→o.
I can sort our input by mapping each colour to a number, with larger numbers for the later colours, then using these numbers to sort the original input. This mapping can be done with an index-of, as I did earlier, mapping colours to positions. Using the examples from the problem specification:
x[⍋'rvbgyo'⍳x←'rybryb']
rrbbyy
x[⍋'rvbgyo'⍳x←'vrvbvg']
rvvvbg
x[⍋'rvbgyo'⍳x←'groovyyoyo']
rvgyyyoooo
x[⍋'rvbgyo'⍳x←'roygbvroygbvroygbv']
rrrvvvbbbgggyyyooo
I can now apply the distance calculation from part 1. For the first example, the re-arranged path takes 32 steps:
+/(⊢+6×0=⊢)6|¯2-/'roygbv'⍳'r',x[⍋'rvbgyo'⍳x←'rybryb']
32
Part 2 – Another Way
While I can re-arrange our input explicitly, this isn’t the only approach to the problem. Stefan Kruger came up with a cool alternative.
Notice that, after re-arranging our input, most of the jumps have distance 6. The only ones that don’t occur when we change group:
(⊢+6×0=⊢)6|¯2-/'roygbv'⍳'rrrvvvbbbgggyyyooo'
6 6 5 6 6 5 6 6 5 6 6 5 6 6 5 6 6
This means that the total distance will be (6×≢x)-a little bit. The extra bit that needs to be subtracted is the total distance from each colour back to the colour that is being jumped to. For example, changing from a group of bs to a group of ys, results in a situation like this:
┌──6──┐
│ ┌2┤
────┐ ┌─4─┐ ┌───
..roygbvroygbvroygbvroygbv..
└──6──┘ └──6──┘
→ b → b → y → y →
Since the first y is a distance of 2 behind the next b, it’s that 2 that we’re subtracting from a maximum jump of 6.
Eventually, we reach the last group, which means we’ve subtracted enough to go from r (which we definitely start on) to the last colour. This means that any group transitions in the middle don’t actually matter! Since we know we’re starting on r, and we know what colour we end on, we know exactly how much to subtract.
If the last group is a group of vs, we only step back one place from r, so we only subtract 1. If the last group is a group of bs, we subtract 2, and so on. If the last group is a group of rs, then there is only one group, and we subtract nothing.
Last group: r v b g y o
Subtract: 0 1 2 3 4 5
I’ve ordered the colours by the order in which they will appear when re-arranged. I want to subtract the corresponding value of the last group, which will be the largest subtraction amount available. Since the subtraction amounts happen to be ⍳6 (with ⎕IO←0), I can find them very elegantly with a lookup:
x←'rybryb'
'rvbgyo'⍳x
0 4 2 0 4 2
⌈/'rvbgyo'⍳x
4
This tells me that the last group (the group of ys) ends 4 steps behind where I would have ended if I took steps of size 6 each time. With this, I finally know the ‘little bit’ to subtract from 6×≢x to obtain the total length of 32:
x←'rybryb'
(6×≢x)-⌈/'rvbgyo'⍳x
32
Isn’t that beautiful! I had a lot of fun solving yesterday’s problem and discussing it with others. Give the next few days of Advent of Code a try, and let us know if you come up with anything interesting!

Follow