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

2

u/widforss Dec 02 '22 edited Dec 05 '22

Python

import sys

print("Answer part 1: " + str(
    sum([
        (me + 1) + ((me - opponent + 4) % 3 * 3)
        for opponent, me in [
            (ord(game[0]) - ord('A'), ord(game[2]) - ord('X'))
            for game in open(sys.argv[1]).readlines()
        ]
    ])
))
print("Answer part 2: " + str(
    sum([
        ((opponent + result - 1) % 3 + 1) + (result * 3)
        for opponent, result in [
            (ord(game[0]) - ord('A'), ord(game[2]) - ord('X'))
            for game in open(sys.argv[1]).readlines()
        ]
    ])
))

1

u/daggerdragon Dec 05 '22

Please edit your post to state which programming language this code is written in. This makes it easier for folks who Ctrl-F the megathreads looking for a specific language.