advent-of-code/2022/python/day02.py

69 lines
1.2 KiB
Python
Raw Normal View History

2022-12-09 16:43:00 +00:00
import shared
from pprint import pprint as pp
rock = "rock"
paper = "paper"
scissors = "scissors"
lose = "X"
tie = "Y"
win = "Z"
Moves = {
2022-12-12 07:41:14 +00:00
"A": rock,
"B": paper,
"C": scissors,
"X": rock,
"Y": paper,
"Z": scissors,
2022-12-09 16:43:00 +00:00
}
2022-12-12 07:41:14 +00:00
Scores = {rock: 1, paper: 2, scissors: 3}
2022-12-09 16:43:00 +00:00
2022-12-12 07:41:14 +00:00
LosesTo = {rock: paper, paper: scissors, scissors: rock}
WinsTo = {paper: rock, scissors: paper, rock: scissors}
2022-12-09 16:43:00 +00:00
def winner(opponent, me):
if opponent == me:
return 3 + Scores[me]
# wins
2022-12-12 07:41:14 +00:00
if (
(opponent == rock and me == paper)
or (opponent == paper and me == scissors)
or (opponent == scissors and me == rock)
):
2022-12-09 16:43:00 +00:00
return 6 + Scores[me]
return 0 + Scores[me]
2022-12-12 07:41:14 +00:00
2022-12-09 16:43:00 +00:00
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)
2022-12-12 07:41:14 +00:00
o_move = Moves[opponent]
2022-12-09 16:43:00 +00:00
pts = winner(o_move, my_move)
score += pts
print(score)
2022-12-12 07:41:14 +00:00
2022-12-09 16:43:00 +00:00
def main():
rows = [row.rstrip().split() for row in shared.load(2)]
run(rows)
if __name__ == "__main__":
main()