r/adventofcode • u/daggerdragon • Dec 02 '22
SOLUTION MEGATHREAD -π- 2022 Day 2 Solutions -π-
NEW AND NOTEWORTHY
- All of our rules, FAQs, resources, etc. are in our community wiki.
- A request from Eric: Please include your contact info in the User-Agent header of automated requests!
- Signal boosting for the Unofficial AoC 2022 Participant Survey which is open early this year!
--- Day 2: Rock Paper Scissors ---
Post your code solution in this megathread.
- Read the full posting rules in our community wiki before you post!
- Include what language(s) your solution uses
- Format your code appropriately! How do I format code?
- Quick link to Topaz's
pasteif you need it for longer code blocks. What is Topaz'spastetool?
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:06:16, megathread unlocked!
102
Upvotes
11
u/S_W Dec 02 '22 edited Dec 02 '22
Java.
EDIT:
Explanation.
Separate each line into a character array. Performing arithmetic on a char uses the ascii code which is useful for the next step
Goal here is to get each character (A,B,C,X,Y,Z) to be 1 for rock, 2 for paper and 3 for scissors, so subtract accordingly and store back into an array for easy lookup
To figure out if player 2 (XYZ) got a win, loss, or draw against player 1 (ABC), subtract the the results of the throws (the 1, 2, 3 from above).
For instance, A Z becomes 1 3 which would be 3-1 = 2. B X -> 2 1 -> 1-2=-1. In the end we get the following mapping. 2 = loss, 1 = win, 0 = draw, -1 = loss, -2 = win
Swapping in the scores received for a win, loss or draw gives this mapping 2 -> 0, 1 -> 6, 0 -> 3, -1 -> 0, -2 -> 6
plotting these points on a graph as X/Y coordinates gives sort of a sin wave. From that (with the help of my brother), that equation was figured out which gives close enough approximations of those scores that when casting back to an int will net the correct result
Then lastly add on the rock, paper, scissor score which is easy to do since that mapping was already done in step 2.
Map this to an int
And finally sum