12-08-2022, 04:28 PM
Post: #9
 DavidM Senior Member Posts: 984 Joined: Dec 2013
RE: Advents of Code (programming challenges)
OK. So here's my version of Day 2 (parts 1 and 2). This didn't have to be solved using a list-based approach, of course, but it's more fun for me to do it that way so I did.

Part 1

The description of the problem needs to be read from the site. Suffice it to say that it is based on the "Rock-Paper-Scissors" game that most probably already know about.

The sample case for this problem is too short to be meaningful (IMHO). The "real" data is 2500 lines of input, and takes a while for even an emulated 50g to solve. So for purposes of this post, I've used only the first 100 lines of the real data provided.

I found it easier to visualize the data if I first converted A, B, and C to R, P, and S. Likewise with X, Y, and Z. So that conversion is first in my solution.

The data needed to be in list form (of course), so the next 2 blocks of code use a similar process to day 1's to get the data in that form.

To provide the requested result, I then convert each "round" to a score, followed by summing each round's result.

Instead of just typing in all of the possible outcomes for the scoring ({ { R R } { R P } { R S } { P R } { P P } { P S } { S R } { S P } { S S } }), I used LCPRD (List Cartesian Product) to build the list:

{R P S} DUP 2 â†’LIST LCPRD

The actual scores are just constants as specified in the problem description.

Here's what I came up with for part 1:
Code:
@ %%HP2: T(3)A(R)F(.)M(=)C(R)B(H); @ Name: 'D2P1' @ Size: 256.5 bytes, CRC: 2F2Ch @ 12/08/22 11:00:00A @ Libraries used: @   1423: ListExt Commands @  \<<     @ place input on the stack     D2dat          @ convert codes to "R P S"     "A" "R" SREPL DROP     "X" "R" SREPL DROP     "B" "P" SREPL DROP     "Y" "P" SREPL DROP     "C" "S" SREPL DROP     "Z" "S" SREPL DROP          @ convert input to list of rounds string     10 CHR "}{" SREPL DROP     "{{" SWAP +     "}}" +     @ convert re-formatted string to list of sublists     STR\->          @ obtain cartesian product of {R P S} against itself     {R P S} DUP 2 \->LIST LCPRD          @ scores for each paired element     {4 8 3 1 5 9 7 2 6} LREPL          @ sum the scores     LSUM \>> D2dat "C Y C Z B Z A Z A Z A Y A Z C Y C Z A Y A Y B X A Y C Z C Z B X C Z A Z B Y C Z A Y C X B Y A Z B Y C Z B Z B Y C Z A Z A Z B Z C Z A X B X C Y C Z C Z C Z A Y C Z C Z C Z C X A Z A Z C Y A Z C Z C Z C Z A Z B Y C Z A Z B Z A Z A Y B X B X C Z C X C Z C Z A Z B Z B X B X B Y C X C Y A Y C Z A Y C Z A X B X B X C X B X B X A Y B Y C Y A Z C Y B Y B X B X B Z B X B Z A Z B Y C Z B Z B Z B Y A Y C Z"

Part 2

Part 2 is essentially a correction to the incorrect assumption stated in the Part 1 problem description.

Having been provided with the real meaning of the "strategy guide", the approach is similar to part 1 but with a different disposition for each round. In particular, the "X Y Z" meanings have changed. The scoring is still the same, though. So a different replacement scheme is created, while still using the same scores as before:
Code:
@ %%HP2: T(3)A(R)F(.)M(=)C(R)B(H); @ Name: 'D2P2' @ Size: 320.5 bytes, CRC: 9274h @ 12/08/22 11:00:00A @ Libraries used: @   1423: ListExt Commands @  \<<     @ place input on the stack     D2dat          @ convert input to list of rounds     10 CHR "}{" SREPL DROP     "{{" SWAP +     "}}" +          @ convert re-formatted string to list of sublists     STR\->          @ convert input to pairs     {{A B C}{X Y Z}} LCPRD     {{R S}{R R}{R P}{P R}{P P}{P S}{S P}{S S}{S R}}     LREPL          @ obtain cartesian product of {R P S} against itself     {R P S} DUP 2 \->LIST LCPRD          @ scores for each paired element     {4 8 3 1 5 9 7 2 6} LREPL          @ sum the scores     LSUM         \>>

The same data is used for both parts 1 and 2. Using the abbreviated data (which is included in the part 1 code listing), the results are as follows:

Part 1: 494
Part 2: 557
 « Next Oldest | Next Newest »