ShareMyScreen/AdventOfCode/2023/02/CubeConundrum/rdm

From J Wiki
Jump to navigation Jump to search

For day 2, I'm dealing with "color cubes" which is to say representations of pixel intensities, where the sample data looks like this:

1sample=:{{)n
2Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
3Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
4Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
5Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
6Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
7}}

If the red, green, blue were expressed as a list of three numbers, I might think of that as:

   game1=: >4 0 3; 1 2 6; 0 2 0
   game2=: >0 2 1; 1 3 4; 0 1 1
   game3=: >20 8 6; 4 13 5; 1 5 0
   game4=: >3 1 6; 6 3 0; 14 3 15
   game5=: >6 3 1; 1 2 2
   game1;game2;game3;game4;game5
┌─────┬─────┬───────┬───────┬─────┐
4 0 30 2 120  8 6 3 1  66 3 1
1 2 61 3 4 4 13 5 6 3  01 2 2
0 2 00 1 1 1  5 014 3 15     
└─────┴─────┴───────┴───────┴─────┘

Here, we can see that game5 has only two "cubes" while the others have three. But this is not a problem for part 1, where the answer I want could be expressed as:

   1 2 3 4 5+/ .*12 13 14*/ .>:"1 >./"2>game1;game2;game3;game4;game5
8

Then again, for part 1, I don't know yet what we will encounter in part 2.

Anyways, the calculation is easy, most of the effort here is parsing the text of the puzzle.

Just like in day 1, I can use ;._2 to parse each line independent of the others. And, I can use i.':' to separate the game number from the cube values. If I append a ';' to the cube value part of the line, I could use another ;._2 to split those, and parse one cube at a time. And if I define adverbs for red, green and blue, I can use J's ". to parse expressions like 1 red, 2 green, 6 blue.

 9cube=: {{ >./ ". y }}
10red=:   {{ ,:1 0 0*m }}
11green=: {{ ,:0 1 0*m}}
12blue=:  {{,:0 0 1*m}}

For example:

   12 red, 13 green, 14 blue
12  0  0
 0 13  0
 0  0 14
   cube '12 red, 13 green, 14 blue'
12 13 14

And, for part 1, what I really want is the game number for the "good cubes", so I throw together a line parser that spits out the game id, or 0 for "games" where the id is too small:

14parse1=: {{
15   max=. cube '12 red, 13 green, 14 blue'
16   sep=. y i.':'
17   id=. 0 ". 4}.sep{.y
18   id**/max >: >./ cube;._1 ';',(1+sep)}.y
19}}

(I guess I used ;._1 with the extra ';' on the left, rather than ;._2 with the extra ';' on the right there, because that was easier to type. And, I didn't use quite the same expressions as my original plan, but this is mathematically equivalent to what I was doing in my initial model.)

((Also, since max is a constant, I could have pulled it out of that definition and made it globally available. But since this only runs once per line, any resulting speed difference would be irrelevant to me.))

Anyways, this gives me:

   parse1;._2 sample
1 2 0 0 5

And, I can finish part 1, like this:

21day2=: ([fwrite&'2003-day2.txt')fread'~/Downloads/input.txt'
22part1=: {{ +/ parse1;._2 y }}
   part1 sample
8
   part1 day2

...

Or I could have gone with +/parse1;._2 sample and +/parse1;._2 day2, but this works.

For part2, I build a different parser. In part2, I no longer care about the game id, instead, I want the product of the maximum of each of the red, green and blue values of a "game"'s cubes. I can re-use the parsing kit I built for part1 for most of this:

24parse2=: {{ */>./ cube;._1 ';',(1+y i.':')}.y }}
25part2=: {{ +/parse2;._2 y }}
   part2 sample
2286
   part2 day2

...

As usual, there are other ways to approach the problem, some more efficient, some more concise.

Also, somewhere around here I went back to my day1 implementation, replacing fwrite with an fread and changing that script to have the line:

<syntaxhighlight lang="J"day1=: fread '2003-day1.txt'</syntaxhighlight>

And, I do something similar here for day2.

I'm not building consumer code here. I don't have to worry about installation and deployment and maintenance. This is disposable code - mostly for my own entertainment.

Possibly though at some point someone is going to arrive here with more curiosity about J than familiarity. If that turns out to be you, the reader of this page, and experimentation doesn't yield the answers you hope for, try leaving a message on the talk page here. Maybe I (or someone else) will be able to provide an answer (though that would not be instant).

day 1 day 3