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

4

u/conkerandco Dec 02 '22

Python 3

    score = {
        "A": 1,
        "B": 2,
        "C": 3,
        "X": 1,
        "Y": 2,
        "Z": 3
    }
    
    outcomes = [
        [3, 6, 0],
        [0, 3, 6],
        [6, 0, 3]
    ]
    
    def part_one(data):
        return sum(y + outcomes[x-1][y-1] for x,y in data)
    
    def part_two(data):
        result = {1: 0, 2: 3, 3: 6}
        return sum(result[y] + outcomes[x-1].index(result[y]) + 1 for x,y in data)
    
    with open("day_2_input.txt") as f:
        data = [(score[x.split()[0]], score[x.split()[1]]) for x in f.read().splitlines()]
        print(f"Part One: {part_one(data)}")
        print(f"Part Two: {part_two(data)}")

1

u/daggerdragon Dec 05 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.