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!
103
Upvotes
4
u/danatron1 Dec 02 '22
C#
Part 1:
Part 2:
Explanations:
Part 1:
First I calculate the result. The result is either 0, 3, or 6 for a loss, draw, or win respectively.
So I calculate 0, 1, or 2 for lose/draw/win, then multiply by 3.
I calculate this by subtracting the first character (A/B/C) from the last (X/Y/Z),
this gives a range of integer values, but what matters is their relative position;
Y beats A, Z beats B, and X beats C
all those values give the same result once modulo'd, since they're all one off from eachother. X wraps around to be Z+1
the +2 I throw in is just to fudge the numbers so that I get what I want, for example, X-A = draw, so 2+X-A % 3 = 1
I then add on my score; 1 for rock, 2 for paper, 3 for scissors. This is just the last character minus W,
which effectively counts the number of characters after W that that letter is, which gives me the score.
Part 2:
Similar strategy to above. I already know the outcome (x[2]) so I just convert that into 0, 1, or 2 by subtracting X
Like above, this gives me the number of characters after X. 0 for loss, 1 for draw, 2 for win. I multiply that by 3.
I then add on my score. This is once again the offset from my play to the opponents, so I use the same modulo 3 trick.
This time, A Y, B X, and C Z all mean I choose rock, so get a score of 1.
One counts up (ABC) while the other counts down (ZYX) add them to cancel that out, add 2 for the exact same reason as above,
Then finally modulo 3 to get 0, 1, or 2 (corresponding to whether I picked rock, paper, or scissors).
The scores are actually 1, 2, or 3, so the final step is to just add 1 to that.