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
View all comments
2
u/jderp7 Dec 02 '22 edited Dec 05 '22
Kotlin
So I was code-golfing my solution a lot and trying various stuff to remove when statements in my Kotlin code and have the solution be purely mathematical, my full solution here: Day 2 Solution
But I am having a bit of trouble wrapping my head around WHY this part
works to score the hand where
firstis the opponent,secondis the player's chosen hand and rock, paper, scissors correspond to0,1, and2as the values.I had worked my way to this after playing with the floorMod of +3 and -3 and noticing that when added together they were basically had the sign of the score I wanted
Individual parts that I DO understand. First off, the function is probably better broken down as
second + 1is the score of the hand that you get just by playing an item. +1 is needed since using 0-based indexing but the scores start at 1(Math.floorMod(first - second, -3) + Math.floorMod(first - second, 3)gives the sign of the win condition (0 for ties, -1 for loss, and 1 for win). This is the part I don't totally get*3is to multiply the previous part in order to change it to 0, 3, or -3 since we will be adding it to get the final score of the victory condition+3is to take the previous part and get the final score. Since scores are not ever negative, this will yield 0 for loss, 3 for draw, and 6 for win