Advents of Code (programming challenges)
|
12-08-2022, 04:28 PM
Post: #9
|
|||
|
|||
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. Commentary about part 1: 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); 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); 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 »
|
User(s) browsing this thread: 1 Guest(s)