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

1

u/Wayoshi Dec 02 '22

Python

score = 0
part1 = False

with open('day02.txt') as f:
    for l in f.read().split('\n'):
        rps1 = ord(l[0]) - ord('A')

        if part1:
            rps2 = ord(l[-1]) - ord('X')
            if rps1 == rps2:
                score += 3
            elif (rps2 - 1) % 3 == rps1:
                score += 6
            score += rps2 + 1
        else:
            if l[-1] == 'X':  # lose: rps2 == (rps1 - 1) % 3
                score += ((rps1 - 1) % 3) + 1
            elif l[-1] == 'Y':  # draw
                score += 3 + rps1 + 1
            else:  # win: rps2 == (rps1 +1) % 3
                score += 6 + ((rps1 + 1) % 3) + 1

print(score)

1

u/daggerdragon Dec 02 '22

Please edit your post to format your code with the backwards-compatible Markdown syntax instead so your code is easier to read on old.reddit and mobile apps.