RogerBW's Blog

Everybody Codes 2026 week 2 21 November 2026

Everybody Codes is now in its third year, and I've been doing it again.

All of my code is on Codeberg.

Day 6

I like sets. And they make this much easier.

As usual in Rust, when I have short strings used as keys I convert them to integers (using base-36) for convenience. (To get them out again for debugging I used the radix_fmt crate but that isn't needed to solve the actual problems.)

Part 1 is straightforward: map from the first item to a set of each of the others. So "BRDS:FLWR,SLTT,YSTS" produces the hash entry:

BRDS → Set(FLWR, SLTT, YSTS)

Then walk the tree (I use a DFS purely to avoid pulling in a VecDeque and because it makes no difference to this whether I go depth or breadth first) and get a list of everything that's referenced, then delete the original.

Part 2 is almost the same, but the mapping is reversed. So from that same line "BRDS:FLWR,SLTT,YSTS" produces:

FLWR → Set(BRDS)
SLTT → Set(BRDS)
YSTS → Set(BRDS)

and then adding "FLWR:SLTT,YSTS" gives an aggregate of:

FLWR → Set(BRDS)
SLTT → Set(BRDS, FLWR)
YSTS → Set(BRDS, FLWR)

The rest of the code is identical.

Part 3 starts with part 2, but I have three separate map-walks to produce the sets of items using each of the three nulls.

Then I intersect each pair of sets, and union that intersection into the output set, thus getting everything that uses two nulls (and deduplicating any three-nulls items automatically as needed).

All of this can be done without a native set type, but it's tedious.

Day 7

Parts 1 and 2 were fairly straightforward: FIFO queue. (The only difference between 1 and 2 is the incoming energy.)

In part 3 the timing was more important, and my "queue" ended up being a hashmap of targets, each with a vec of incoming impulses. So the round ended up being:

  • process the incoming queue, adding energy and putting new impulse entries on a new outgoing queue.

  • scan each cloud looking for explosions. If one is found, generate more impulses on that same outgoing queue, and add that cloud ID to the deletion list.

  • delete each exploded cloud from the hash of clouds, and from the outgoing queue.

  • exit if the outgoing queue is empty, otherwise loop again.

The final version took a while to run so I could probably do it more efficiently, but I took a chance with the maximum found by the 12th ID (in random order, hash keys after all) when about 20 IDs had been calculated, and that turned out to be the answer. (If it hadn't been, I'd have let the code run to completion — as I did — and put in the final value.)

Day 8

Definitely one where I could have done better by building a proper hex-grid data structure. But I didn't. Too bad.

I did assume I'd be looking at different directions of light, and that was indeed the case. So I split out the shade calculator into its own function. Not that it did very much yet.

Part 2 was the biggest slog. An enum for directions is all very well, but the horizontal passes (east/west) need to start at the right point and then jump by two in X. Probably some of the code I put in to achieve this is unnecessary; the important bit is the "look along the row until you find the first thing that exists, then step two from each one after that".

In part 3 I had the realisation that I could treat a seed as a tree of height -2, which grows each round whether lit or not. Then it was just a matter of passing a mutable reference to the field all the way down into the shade calculator, and tracking the count of new warriors as well as energy. (There's commented-out code for displaying the field too, as I used it in debugging.)

Day 9

Some serious thinking needed this time.

A Plant structure with a grow function and an elements function seems like the way to do this, and that served me well all through the problem. The first part was mostly fiddling with the structure to get the right results; what I ended up doing was storing a Counter of "twigs" (segments with no divisions, stored by length) and a count of "centres" (junction points). Then, when twigs equal or exceed the growth limit:

  • note the count of twigs, ct
  • add centres equal to ct
  • remove these twigs
  • add ct×2 twigs of half the length
  • add ct twigs of length 1

The number of elements is the number of centres, plus the total length of all the twigs.

For part 2, I just need a different elements function, and a bit of inspection reveals that the number of leaves equals one more than the number of centres.

For part 3, I looked around for a purely mathematical solution, but while I found some interesting correlations with the growth=3 layout I wasn't able to generalise it. So instead I built in a skip: if the longest twig is length A, and a split happens when it hits length B, then I can skip forward by B-A frames, increment all twig lengths by that amount, and then resolve the growth as above.

Day 10

I borrow "Coord" and "Dir" classes from previous years.

For the first part, I establish field, a HashSet of valid spaces to move into, and ghosts, a Vec of the Ghost type, which contains a position and a direction. (A Ghost starting space is necessarily in the field.)

Then for each cycle I perform two operations:

(1) For each ghost, test whether the position in front of it exists; if so move it, if not turn it and score a collision.

(2) Sort the ghosts into a HashMap keyed by their current position (and containing Vecs of their directions, since that's all I need to know). For each entry, if there's one ghost, just make a new one; if there are more, score collisions equal to the number, reverse the directions if there's an even number of ghosts involved, and write out the new ones.

In part 2 I was deceived by the statement All the movement and collision rules from Part I remained exactly the same. The number of collision events that happen in an N-ghost intersection is now 1 rather than N. But my test data passed by the old rules, so that took a bit of digging.

For part 3, well, it ended up looking quite like yesterday's problem with the need to skip forward to significant events; in this case if a list of ghost B matches a list A from an earlier cycle, I can look through them to see how many more cycles I'll need to run before any ghost hits its collision limit (and then simulate them more cheaply). Things I learned here:

  • using std::iter::zip to run through two lists side by side rather than just using an index value.

  • using std::hash to build a custom hasher on the Ghost structure, so that it would give ghosts with equal position and direction the same hash key even if the number of collisions were different.

  • rewriting the ghost to ghost collision detection logic from parts 1-2, so that it would preserve the order of the ghosts (so that I could quickly compare two lists of them, can't use a hash as a hash key).

And, in case the data get huge, I clear the cache of previously-seen ghost positions whenever a ghost is removed.

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 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 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