advent-of-code/2022/python/day02.py
2022-12-12 02:41:14 -05:00

69 lines
1.2 KiB
Python

import shared
from pprint import pprint as pp
rock = "rock"
paper = "paper"
scissors = "scissors"
lose = "X"
tie = "Y"
win = "Z"
Moves = {
"A": rock,
"B": paper,
"C": scissors,
"X": rock,
"Y": paper,
"Z": scissors,
}
Scores = {rock: 1, paper: 2, scissors: 3}
LosesTo = {rock: paper, paper: scissors, scissors: rock}
WinsTo = {paper: rock, scissors: paper, rock: scissors}
def winner(opponent, me):
if opponent == me:
return 3 + Scores[me]
# wins
if (
(opponent == rock and me == paper)
or (opponent == paper and me == scissors)
or (opponent == scissors and me == rock)
):
return 6 + Scores[me]
return 0 + Scores[me]
def which_move(opponent, me):
if me == lose:
return WinsTo[Moves[opponent]]
if me == win:
return LosesTo[Moves[opponent]]
if me == tie:
return Moves[opponent]
def run(moves):
score = 0
for move in moves:
opponent, me = move
my_move = which_move(opponent, me)
o_move = Moves[opponent]
pts = winner(o_move, my_move)
score += pts
print(score)
def main():
rows = [row.rstrip().split() for row in shared.load(2)]
run(rows)
if __name__ == "__main__":
main()