Skip to content
advent of code 2022

day 02

rock paper scissors

approach

each round is a pair of letters, and both are turned into 0, 1 or 2 with ord. with the shapes numbered that way, the shape that beats x is (x + 1) % 3 and the one that loses to it is (x - 1) % 3.

part 1 reads the second column as a shape and scores the round by comparing the two numbers. part 2 reads it as the outcome instead and works out which shape to play from the opponent's number, plus or minus one mod 3.

the rust version skips the arithmetic and spells out all nine cases for each part in a match.

the trick

number the shapes 0–2 and "beats" becomes "is one more, mod 3".

python
def part2(pred, outcome):
    o_pred = ord(pred) - ord('A')
    if outcome == "X":
        return 0 + ((o_pred - 1) % 3) + 1
    elif outcome == "Y":
        return 3 + o_pred + 1
    else:
        return 6 + ((o_pred + 1) % 3) + 1