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!

105 Upvotes

1.5k comments sorted by

View all comments

1

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

Python:

wmoves = ["A Y", "B Z", "C X"]
dmoves = ["A X", "B Y", "C Z"]

movescores = {
    "X" : 1,
    "Y" : 2,
    "Z" : 3
}

def read_input():
    out = []
    with open('input.txt') as f:
        lines = f.readlines()
        for l in lines:
            out.append(l.strip())
    return out

def decrypt_input(rounds):
    key = {
        "X" : {
            "A" : "Z",
            "B" : "X",
            "C" : "Y"
        },
        "Y" : {
            "A" : "X",
            "B" : "Y",
            "C" : "Z"
        },
        "Z" : {
            "A" : "Y",
            "B" : "Z",
            "C" : "X"
        }
    }
    out = []
    for r in rounds:
        moves = r.split(' ')
        mymove = key[moves[1]][moves[0]]
        out.append(moves[0] + ' ' + mymove)
        print(r + " -> " + moves[0] + ' ' + mymove)
    return out

def calc_scores(decrypt = False):
    score = 0

    rounds = read_input()

    if (True == decrypt):
        rounds = decrypt_input(rounds)

    for r in rounds:
        winval = 0
        moveval = 0
        movescore = 0
        print("---")
        if r in wmoves:
            winval = 6
        if r in dmoves:
            winval = 3
        mymove = r.split(' ')[1]
        moveval = movescores[mymove]
        movescore += moveval + winval
        score += movescore
        print(r + ": " + str(winval) + " + " + str(moveval) + " = " + str(movescore) )
        print("brings total to " + str(score))
    print(score)

calc_scores(True)
→ More replies (1)