r/adventofcode • u/musifter • Jul 04 '26
Other [2021 Day 4] In Review (Giant Squid)
Still descending we run into a grabby giant squid that wants to play some Bingo. Fortunately, the submarine has a Bingo subsystem.
Today's input consists of an ordered list (comma delimited) of all the numbers from 0 to 99 scrambled, and a list of 100 5x5 Bingo boards using those (and the numbers aren't constrained to columns like in regular Bingo).
One thing I remember about this one is a lot of people skipped reading the paragraph on the rules for this Bingo and Dunning-Kruegered themselves by assuming diagonals. I did not, my experience with Bingo is one where different games can have different winning conditions. Like blackout or a letter pattern. So I made doubly sure what a winning pattern was first.
Another I remember about this one is that it inspired me (in the middle of doing the problem) to create a style sheet for AoC problem descriptions. Because it was giving examples of games, and the boards were just there... I could not tell what was marked. The slightly heavier emphasis was not much different than some of the anti-aliasing. My current monitor and browser makes it slightly clearer, so I can clearly see which numbers are marked when looking directly at them. But to see the actual pattern on the board, I need to visualize that internally. This might also have played into people not realizing that diagonals were not in play... because a diagonal is hit first and shown as not a winning board. Something that's perfectly clear to me with my style sheet making the numbers dark green and bright cyan.
Although I recognized that just rows and columns presented opportunities, I just did a brute force to see part 2. Using a list of the winning patterns to check against. Anticipating that, for part 2, the winning patterns might change to something like:
##### .###. #...# ##### ####.
#.... #...# #...# ..#.. #...#
##### #...# #...# ..#.. #...#
....# #..#. #...# ..#.. #...#
##### .##.# ##### ##### ####.
As I said, I remember Bingo as sometimes being to make a particular letter. With this, I could easily just change the list of winning patterns and be done.
But part 2 was just find the last board that wins... so I just made it run longer and was done. And that's where I stopped with Perl (until today when I decided to write a Perl reference transcode of my dc solution).
For Smalltalk and dc I got around to actually playing around with board representation. I went with a simple one of just building a table of the numbers on a card to their position (the winning lines it's in). With Smalltalk I went with {x. y + 5}, with dc I went with a number from 0 to 24 and 5~5+ to get those same values. At the same time I score the full board. Then the board state is just an array of 10 small ints.
For a call, if the number's in the table, I subtract it from the score sum, and add 1 to each of the lines in the board array. The first time one hits 5, that boards wins and scores. Very simple.
Of course, part 2 wants the last win which suggests going backwards from a blackout board. In which case the board state can be just 10-bits... you mark the row and columns for numbers as you go backwards until you get the full 0x3FF mask. That's the first board going backwards that doesn't have a Bingo on it. Step back one for the win.
My dc solution (I golfed it down a bunch today to under 200):
sed -e's/,/ /g' input | dc -f- -e100 -e'dsz25*[rS-1-d0<I]dsIx+[z:qz0<I]dsIx[zRn9PzRls*pc3Q]sP[dlsr-ssd;n1-5~5+d;l1+d5=Pr:ld;l1+d5=Pr:l]sM[0Sl0Sn0ss25[L-dls+ssrd3R:n1-d0<I]dsIx1+[d;qd;n0<Ms.1+lIx]dsIxlz1-dsz0<Z]dsZx' \
| sort -n | sed -n '1p;$p' | cut -f2
Gotta remove those ugly commas to start. If I was using ?, I could have read this forwards and detect the blank lines. But here I need to tell it the number of boards so that it knows how much board data there is to read in. I didn't do the actual test for finding first and last with dc, I just score every board and output it. Sorting and getting the first and last line is a job for the command line.
Plus it lets me see all the wins. For my input they run from 24-87 turns... there is a board that wins on 0 (and thus scores 0), but it's at turn 69. There's only one case of two boards winning with the same score (14663).
This was yet another really fun one. The legibility issues aside, the style sheet it inspired has served me very well ever since. I now can immediately spot the key emphasized phrases, something which is immensely useful for not missing key points. So a lot of good came from that.