My (boring) day 3 post
http://jsoftware.com/pipermail/programming/2021-December/059450.html
puzzle page for day 4 https://adventofcode.com/2021/day/4
I had thought I remembered posting about day 3 previously, and I left
out some probably necessary description because of that. However, on
reviewing my sent mail, I see that I did not. (Sorry about that.)
Anyways, day 4 was a sort of a "bingo-like" game, ignoring diagonals.
(You can see that things are still pretty easy here... they get
harder, later.)
Here's the sample data:
sample=:bingoboard {{)n
7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
22 13 17 11 0
8 2 23 4 24
21 9 14 16 7
6 10 3 18 5
1 12 20 15 19
3 15 0 2 22
9 18 13 17 5
19 8 7 25 23
20 11 10 24 4
14 21 16 12 6
14 21 17 24 4
10 16 15 9 19
18 8 23 26 20
22 11 13 6 5
2 0 12 3 7
}}
In other words, a line of numbers which would be called, and three
"bingo boards".
The first part of the puzzle is to determine which of the three boards
wins first (and you show you figured this out by reporting the board's
"score" (the sum of all unmarked numbers on the board, multiplied by
the last number to be called when it won).
My parser looked like this:
bingoboard=:{{
seq=: ". (y i. LF){. y
boards=: _&".;._2;._1 }.rplc&((LF,LF);LF,'/') (y i.LF) }. y
seq;boards
}}
(Find the end of the first line, those numbers go in one box. Convert
blank lines to / and use that as a leading delimiter to turn the rest
of the text into a rank three array.)
My solution for the first part had an error which I did not notice
until I got to part 2. Sometimes buggy code seems to work right. (I
was negating the values on the board when they were called, but
there's a 0 on two of the boards.)
Still, it worked well enough for the first part:
score=:{{
'seq boards'=. y
for_s. seq do.
boards=. boards * _1^boards=s
win=. checkwin"2 boards
if. 1 e. win do.
s*+/0>.,win#boards return.
end.
end.
__
}}
checkwin=:{{
+./*/0 > y,.|:y
}}
The second part of the puzzle is to find the last board that wins and
report its score. I added one to each of the values to work around my
error (and subtracted 1 as the first step in reporting the score):
lastscore=:{{
'seq boards'=. 1+each y
for_s. seq do.
boards=. boards * _1^boards=s
win=. -.checkwin"2 boards
if. -.1 e.win do.
(s-1)* +/,(<: * 0<])prevwin#boards return.
end.
prevwin=. win
end.
__
}}
(I know that code golf is fun, but it's not always necessary, and here
I used J's implicit parallelism where it was easy and primitive
control structures where that made sense to me.)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm