2022/day02: complete!

day07
earnest ma 2022-12-02 22:20:16 -05:00
parent 85107c66c3
commit 19b014b090
Signed by: earnest ma
GPG Key ID: A343F43342EB6E2A
3 changed files with 2614 additions and 0 deletions

99
2022/day02.py Normal file
View File

@ -0,0 +1,99 @@
#!/usr/bin/env python3
# opponent: a rock, b paper, c scissors
# ME : x rock , y paper, c scissors
# Score: 1 R 2 P 3 S
# + 0 lost 3 draw 6 won
# same: draw
# Rock over scissors
# Scissors over paper
# paper over rock
scores = {"X": 1, "Y": 2, "Z": 3}
plays = {
"A": "X",
"B": "Y",
"C": "Z",
"Z": "C",
"Y": "B",
"X": "A",
} # rock # paper # scissors
must = {"X": "lose", "Y": "draw", "Z": "win"}
def choose(opponent: str, ending: str) -> str:
"""
>>> choose("A", "X") # rock, must lose
"C"
"""
if ending == "Y": # draw
return plays[opponent]
if opponent == "A":
if ending == "X": # lose
return "Z"
else: # win
return "Y"
if opponent == "B":
if ending == "X":
return "X"
else:
return "Z"
if opponent == "C":
if ending == "X":
return "Y"
else:
return "X"
def get_score(play: str) -> int:
opponent = play[0]
us = play[2]
if plays[opponent] == us: # draw
return 3 + scores[us]
if us == "X": # rock
if opponent == "C":
return 6 + scores[us]
else: # loss
return scores[us]
if us == "Y": # paper
if opponent == "A": # rock
return 6 + scores[us]
else:
return scores[us]
if us == "Z": # scissors
if opponent == "B":
return 6 + scores[us]
else:
return scores[us]
def part1(strategy) -> int:
score: int = 0
for line in strategy:
score += get_score(line.strip())
return score
def part2(strategy) -> int:
score: int = 0
for line in strategy:
line = line.strip()
score += get_score("{0} {1}".format(line[0], choose(line[0], line[2])))
return score

2500
2022/day02.txt Normal file

File diff suppressed because it is too large Load Diff

15
2022/day02_test.py Normal file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env python3
from day02 import *
def test_part1() -> None:
strategy = open("day02.txt")
assert part1(strategy) == 10595
strategy.close()
def test_part2() -> None:
strategy = open("day02.txt")
assert part2(strategy) == 9541
strategy.close()