r/adventofcode • u/musifter • Oct 25 '26
Other [2024 Day 25] In Review (Code Chronicle)
Since we're out of places to search, we return to check the Chief's office check it again. However the door is locked... someone is inside (no prize for guessing who), but they aren't answering our knocks. And so we need to open the Virtual Five-Pin Tumbler lock ourselves. Security can supply us with the schematics for every lock and key, but to make to make sense of them, we make our yearly call to customer support. They tell us that we can rule out some combinations based on if a key and pin in a position would overlap each other. Naturally, once they find out how many of each we have (250 of each), they start to get wise to who's calling and so we have to hang up to avoid questions. As usual.
The input consists of 250 locks and 250 keys. They aren't labelled as such (or in order), we just get 500 5x7 ASCII grid bitmaps. Which is which is identified by the top and bottom lines (top full/bottom empty is lock, top empty/bottom full is key). We want to calculate how many pairings between them fit together without an overlap.
And for a quick first solution, I just read them into @lock and @key arrays of the counts of # in each of the 5 columns and compared the sums:
foreach my $lock (@locks) {
foreach my $key (@keys) {
$part1++ if (all {$key->[$_] + $lock->[$_] <= 7} (0 .. 4));
}
}That got the answer quickly, and 4s later I had star 50, because I remembered to wait to read the story. Which is that the Chief was sleeping and has been here all along, except for when he went for coffee a month ago. Fortunately our work isn't completely lost, because it helps the Chief finish the chronicle he's writing for Santa.
Of course, there are lots of ways to do this problem. And since the input is bitmaps, all of the same size, a straight up bitmap AND will also catch overlaps. And so, my refined solution:
foreach my $item (map {join('', map {y/.#/01/; $_} split /\n/)} <>) {
push( $items[ substr($item, 0, 1) ]->@*, oct("0b$item") );
}
my $part1 = 0;
foreach my $lock ($items[LOCK]->@*) {
$part1 += grep {($_ & $lock) == 0} $items[KEY]->@*;
}I actually did this version in Smalltalk first, and was still focused on the columns when doing this and initially transposed the input... but of course, the direction of the raster makes no difference with this approach. I think I also got caught up in thinking of how to do transpose in Smalltalk:
" Takes an NxM table and transposes it to an MxN "
transpose [
^(1 to: self first size) collect: [:col | (self collect: [:row | row at: col])].
]Next up was doing a dc solution. And I did a few (all using the old v1.4.1 version of ? to read in line-by-line and detect the blanks)... for example, I did some involving bitmap AND. Those were slower (but shorter) than my original dc solution though. Which was inspired by day 24:
echo "Original (add overflows): "
echo -n "Part 1: "
tr '.#' '01' <input | dc -e'[lp1+sp]sC[[lk1+dsk:k]sA]sKC8ss2i?[[ll1+dsl:l]sA0=K0d?[rd2r^3R[2~3Rd3R*5R+_4Rls*rd0<B]dsBx*+1+?z2<L]dsLxs.lAx?z0<I]dsIxAill[lk[d;k3Rd;l3R+0r[64~r2~4R+3Rs.rd0<R]dsRx+0=Cr1-d0<K]dsKx+1-d0<L]dsLxlpp'What this does is read the lock/key into a 35-bit number, using (and disposing) of the first line to tell which array to put it in. The bits are a bitmap of the object by column, with 0s in between. So when we sum a lock and key, if a carry ripples into one of those overflow bits, we have a collision.
Here's an example, using the 2nd and 3rd items in the test (a lock and key respectively):
00000010000011000000000111110000111 (2nd in test)
01111110100000011100001100000111100 (3rd in test)
v v v v v
10000000100011011100010011111000011In a language with actual bitwise operators, the final check would be an AND with bitmask of those target bits, but dc can't do that, so we loop and chew up the number, adding every 7th bit together and seeing if that's still 0 at the end.
And so we come to the end of the the 10th year, and the last of the 25 day format. During this year I wasn't in the best shape, which made me anxious at first after the difficulty level I remembered from 2023. But in the end, it wasn't as hard as that year. And my approach of just going keeping things simple resulted in overall good times for me.
2
u/e_blake Oct 26 '26
My initial solution converted each line of input into a hex number (add a prefix 0x, translit . to 0 and # to 1); adding 7 lines together gives me a 5-column hex value with digits between 1 and 6 for each key and lock. Adding a key and lock together, then masking with 0x88888, produces 0x00000 if they are compatible, and nonzero if any column overflowed larger than 7. This finishes in under 200ms, so I never really optimized it further, even though my golf solution takes a different approach.
However, it was a couple more days before I got my second star, because I finally had time to mop up all the partial solutions I had left behind in earlier days. In fact, my 49th star came from day 6. Although I didn't start this year in December, I was quite pleased that I did get all 50 stars in m4 in under a month from when I first solved day 1 in March 2025. It was also nice that the entire year's solutions run single-threaded serially in less than 1 minute cumulative runtime; not as fast as some other years but not as bad as 2015-2016 with expensive md5 computations. The theme of revisiting favorite spots from 9 earlier years was also nice for a decade of advent of code.
4
u/terje_wiig_mathisen Oct 25 '26
My initial Perl code counted pins/tumblers in each column, then checked that the total was less or equal to 5 on all.
After several Cogniters finished the year we started coming up with optimizations, personally I implemented 4 or 5 different algorithms in Rust:
- I had high hopes for a parser which skipped the first 4 bytes in each key/lock, then collected the next 32 into a SIMD register, compared against a full set of '#' marks for equality and extracted the top bits as a u32, but as long as this was a standalone unsafe function it actually runs fractionally slower than pure Rust!
In the end I therefore settled on the simpler
I saved all those u32 keys/locks in a single Vec<u32>, allocated to be large enough to hold the total input, but with keys and locks saved from alternate ends:
Finally I process 8 lock/key combinations per iteration, this makes it abundantly clear to clang that the code should be vectorized with AVX instructions.
Final runtime is in typical u/ednl territory, 2142 ns or 2 us. u/maneatingape lists 8 us.
This was definitely an easier year than 2023, it was also the first year that another Cognite co-worker managed to beat me on the company leaderboard, simply by getting up every morning at 06:00 and then on average solve the puzzles marginally faster. I blame getting old & slow!
3
u/e_blake Oct 25 '26
My golfed m4 solution is 149 bytes but 6 seconds runtime, using bitwise comparison of all 500*499/2 blocks with one another (slower than sorting into keys vs. locks for only 250*250 comparisons, but golfs denser, and comparing two keys or two locks fails in the same manner as a key and lock that don't fit together).
3
u/ednl Oct 25 '26
Unlike /u/terje_wiig_mathisen I don't know much about vectorisation and how best to optimise for it. So my solution is oldskool:
Clang says it can only vectorise the very last loop:
Runtimes: M4 4.87 µs, M1 6.87 µs, Pi5 19.8 µs. https://github.com/ednl/adventofcode/blob/main/2024/25.c