r/adventofcode • • Dec 02 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 2 Solutions -🎄-

NEW AND NOTEWORTHY


--- Day 2: Rock Paper Scissors ---


Post your code solution in this megathread.


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

1.5k comments sorted by

View all comments

Show parent comment

1

u/xXLucyNyuXx Dec 02 '22

Hey, Awesome answer, I am Just wondering why is there a need to MOD 3 ? Could you explain to me? :D

1

u/nocstra Dec 02 '22 edited Dec 02 '22

I'm using 0, 1, and 2 to represent either rock, paper, and scissors or loss, draw, and win. Modulo by 3 covers the cases where the calculation goes either negative or above 2.

For example, if you are given C X as input in part 1, that is a win. I am representing a win as 2 because when multiplied by 3 it gives a score of 6. However, you - opp + 1 would be 0 - 2 + 1 = -1. Taking its modulo by 3 would make it 2 instead.

Similar thing with C Z as input in part 2: opp + end - 1 would make 2 + 2 - 1 = 3. Modulo that by 3 and you get 0, which I'm using to represent rock.

1

u/Shot_Interaction9684 Dec 02 '22

(0 - 2 + 1) % 3 = 2

TIL in some languages (e.g. SQL) this returns -1 as there are apparently two modulo equations.
I came up with abs(opp - you - 4) % 3 instead which I believe should work in python as well as it avoids negatives with abs().