Everybody Codes is now in its
third year, and I've been doing it again.
All of my code is on Codeberg.
[Day 16](https://everybody.codes/event/2026/quests/16
Parts 1 and 2 are straightforward: just do the actual swaps.
For part 3 I turn the problem inside-out and resolve each character
separately.
I'll work it through with two copies of the test input:
RRYGGYBBYGRRYGGYBBYG
Take the first character of the shelf, and start an empty list of
remnants.
Establish left and right pointers.
Start the loop. While left and right don't overlap:
Step left rightwards until it hits a character other than the active
one, or hits right.
Step right leftwards until it hits the active character. Push each
skipped character onto the remnants list (so they are now in reverse
order).
RRYGGYBBYGRRYGGYBBYG
L R
remnants = GYBBYGGY
If left and right don't overlap, score the difference between them as
a count of reversed items. (Don't actually swap anything.) (Example:
score 9.)
Now step right leftwards until it doesn't hit the active character.
And step left rightwards until it does (and in this one case is
allowed to overlap the right pointer). And push those skipped
characters onto the remnants too (in forward order; these are the ones
that have notionally been reversed, from the first non-active character).
RRYGGYBBYGRRYGGYBBYG
L
R
remnants = GYBBYGGYYGGYBBYG
If the pointers haven't overlapped, score the difference again.
(Again, don't actually swap anything.)
Keep doing that loop until the pointers have overlapped; that will
have dealt with all the occurrences of that character. Then reverse
the remnants so that they're all back in their post-reversal order,
use them as the new shelf, and start again with the next distinct
character.
[Day 17](https://everybody.codes/event/2026/quests/17
Part 1 was clearly a straightforward Dijkstra, so for amusement I
thought I'd try the pathfinding crate's Grid struct. This is
basically a bunch of convenience methods for a rectilinear grid (e.g.
automatically adding connections when you have two adjacent cells).
And that was quite fun.
For part 2 I had a moment of inspiration, which was to inflate the
grid. Each node in the original becomes a 3×3 array in the transformed
version. Within that array, all corners of set nodes always exist.
Centres never exist. Each edge only exists in a set node if there is
no neighbour in that direction.
So if I started with
#.#
#.#
###
this would be expanded to a set of corners:
#.#...#.#
.........
#.#...#.#
#.#...#.#
.........
#.#...#.#
#.##.##.#
.........
#.##.##.#
and then the edges filled in:
###...###
#.#...#.#
#.#...#.#
#.#...#.#
#.#...#.#
#.#...#.#
#.#####.#
#.......#
#########
The corner of a cell and the adjacent corner of its neighbouring cell
occupy the same notional position. So the only nodes that count for
calculating travel distance are the edge nodes (which are the ones for
which either coordinate modulo 3 is 1).
In theory there could be disjoint loops which aren't reachable from
the starting point (e.g. if there were a surrounded space in the
original grid), but we're told that all edges form a single loop, so
for part 2 I simply count those edge nodes.
And for part 3 I run a Dijkstra from corner to corner, but moves onto
edge nodes count as a cost of 1, and moves onto corners count for
zero.
[Day 18](https://everybody.codes/event/2026/quests/18
An interesting progression of complexity here, very much in the Advent
of Code spirit.
For part 1: I have a vec of heights. For each height from 1 to the
maximum, I find the first and last column at or above that height, and
set all columns in between to that height if it's not already higher.
Then take the sum of all column heights.
For part 2, basically the same idea, but I use the width parameter to
set height on a range of columns.
For part 3 (which will work on all the previous problems too, as long
as width defaults to 1) I take a completely different approach, which
I call "sinking". Instead of a vec indexed by column number, I build a
vec of (column number, height), in order, setting only the left and
right edges of each column (because they're the only ones that will
matter). I sort them by column number, because this relies on them
being in order, though in my data they are anyway.
Then initialise total area to zero and start a loop. In each pass:
-
take the lesser height of the first and last entries (leftmost and
rightmost column heights)
-
take the horizontal distance between them (+1) as the active width.
-
add height × width to the total area. (It doesn't matter whether any
given cell was an original element or is being added to the mould,
and so we don't bother to check.)
-
filter the list of columns, keeping only those that are higher than
this height and subtracting the height from them.
-
make that the new column list, and repeat until no columns are left.
The complexity of this is based on the number of distinct heights and columns,
rather than the total width.
So using the part 2 example, leaving gaps for the internal columns
when width is 3 or more:
.......#.#.#....................
.......###.#....................
.......###.#....#...............
.......###.#....#..............#
#......###.#....#........#.....#
##.#...###.#....#........#.....#
##.#.#####.##...#..#...#.#.....#
##.#.#####.##...#..#...#.#.....#
##.#.#####.##...#..#...#.#...###
The first pass finds a height of 5, and a width of the full 32-column span. So
we will notionally fill that ("A", scoring 32 × 5 = 160):
.......#.#.#....................
.......###.#....................
.......###.#....#...............
.......###.#....#..............#
#AAAAAA###A#AAAA#AAAAAAAA#AAAAA#
##A#AAA###A#AAAA#AAAAAAAA#AAAAA#
##A#A#####A##AAA#AA#AAA#A#AAAAA#
##A#A#####A##AAA#AA#AAA#A#AAAAA#
##A#A#####A##AAA#AA#AAA#A#AAA###
Then chop out all those rows and keep what's left:
.......#.#.#....................
.......###.#....................
.......###.#....#...............
.......###.#....#..............#
The now-empty columns off to the left are automatically ignored.
#.#.#....................
###.#....................
###.#....#...............
###.#....#..............#
The next min(first, last) height is 1, and we fill that width 25 with
"B", scoring 25, total 185:
#.#.#....................
###.#....................
###.#....#...............
###B#BBBB#BBBBBBBBBBBBBB#
Slice off the empty space on the right and we get height 1, full
width 10, fill with "C", scoring 10, total 195:
#.#.#.....
###.#.....
###C#CCCC#
Finally fill the remaining height 2 width 5 space with "D", scoring
another 10, total 205:
#D#D#
###D#
[Day 19](https://everybody.codes/event/2026/quests/19
For part 1 I started off coding up the two-dimensional grid, then
read the problem statement again and decided to pull it down to just
one. All that matters is lining up the cells in layer 0 and layer 1,
not how they relate to each other within the layer.
For part 2, I can run through all the possible permutations (with
memoize to keep things fast).
Part 3 felt like a bit of a let-down to me, because it didn't really
have anything to do with the individual colours, didn't need
memoisation, didn't even need the Counter class. Yes, it requires
mental agility, but once that's done the programming is trivial.
First I make an assumption, that black is not a significant proportion
of the total. Then as long as the numbers of the other colours aren't
wildly different I can set up any combination of outputs I like. I
know that the product of numbers with a set sum is highest when those
numbers are equal. So I take the count of non-black cells, divide that
as evenly as possible into three parts, and return the product.
[Day 20](https://everybody.codes/event/2026/quests/20
The only unnecessary sophistication for part 1 was building a
multi-digit parser. Which turned out to be handy in the later parts
anyway.
Part 2 needs very little extra, just a change to the parser and an
output filter.
Part 3 was frustrating because I tried to do it the easy way. It turns
out that if you incorrectly assume that all shapes with the same
number of segments are reachable from each other in a single move
(specifically that 2 and 5 are mutually reachable), your code will
correctly pass both the test cases and give a very convincing-looking
but wrong answer to the live data, because it accepts exactly one line
that it shouldn't. (At least in my live data.) I ended up modifying
someone else's code to produce a debugging output so that I could work
out where I'd gone wrong.
What I should have done, of course, is work out the groups
programmatically by comparing bitmaps of segment patterns. Bit count
would give segment count, the "one move" pairs would be the ones with
the same segment count but 2 bits set when XORed together, and "plus
one" would have +1 on the segment count and 1 bit set on the XOR. (I
have not tested this.) And of course make sure the "+/-" group is
disjoint from "1". I hope this is the approach I'd take for "serious"
programming. But instead in this case I did it by hand, and that was
my error.
Conclusions
I continue to be reasonably comfortable in the bits of Rust I'm happy
with, though I didn't need to move outside that space much. I think I
did more with references and less with cloning/copying than last time.
I did finally get away from my traditional index-based list iteration
to using the built-ins like zip and skip.
I didn't do any flashy input parsing; almost everything could be done
with split (and indeed day 3, the one time I did use a regexp parse,
could easily have been done that way too).
Crates used:
- pathfinding, five times
- counter, twice
- itertools, once
- memoize, once
- regex, once
- topological-sort (it turns out
pathfinding can do this too)
My "global rank" (order of finishing part 3) rose from the 600s (ten
days after day 1) to the 100s (a day and a half after day 20). As I
write, only 867 people have solved even one part of one puzzle. I
don't know why these challenges aren't more widely known. If you find
Advent of Code fun, I strongly suggest you take a look at this too.
It's a shame that all the login options, required to participate, are
still with hateful organisations, but Advent of Code has the same
problem.
I'll certainly be back next year.
Thanks as always to Emil
Kaczyński,
and the EC team.