RogerBW's Blog

Everybody Codes 2026 week 4 02 December 2026

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.

Add A Comment

Your Name
Your Email
Your Comment

Note that I will only approve comments that relate to the blog post itself, not ones that relate only to previous comments. This is to ensure that the blog remains outside the scope of the UK's Online Safety Act (2023).

Your submission will be ignored if any field is left blank, but your email address will not be displayed. Comments will be processed through markdown.

Search
Archive
Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 2300ad 3d printing action advent of code adventure aeronautics aikakirja anecdote animation anime army astronomy audio audio tech aviation base commerce battletech bayern beer boardgaming book of the week bookmonth chain of command children chris chronicle church of no redeeming virtues coding challenge cold war comedy computing contemporary cornish smuggler cosmic encounter coup covid-19 crime crystal cthulhu eternal cycling dead of winter disaster doctor who documentary drama driving drone ecchi economics en garde espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 essen 2022 essen 2023 essen 2024 essen 2025 essen 2026 everybody codes existential risk falklands war fandom fanfic fantasy feminism filk film firefly first world war flash point flight simulation food garmin drive gazebo genesys geocaching geodata gin gkp gurps gurps 101 gus harpoon historical history horror horrorm science fiction hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo 2021 hugo 2022 hugo 2023 hugo 2024 hugo 2025 hugo 2026 hugo-nebula reread humour in brief avoid instrumented life javascript julian simpson julie enfield kickstarter kotlin learn to play leaving earth linux liquor lovecraftiana lua mecha men with beards mpd museum music mystery naval noir non-fiction one for the brow openscad opera parody paul temple perl perl weekly challenge photography podcast poetry politics postscript powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant review reviews romance rpg a day rpgs ruby rust scala science fiction scythe second world war security shipwreck simutrans smartphone south atlantic war squaddies stationery steampunk stuarts suburbia superheroes suspense talon television the resistance the weekly challenge thirsty meeples thriller tin soldier torg toys trailers travel traveller type 26 type 31 type 45 typst vietnam war war wargaming weather wives and sweethearts writing about writing x-wing young adult
Special All book reviews, All film reviews
Produced by aikakirja v0.1