-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.py
82 lines (68 loc) · 1.84 KB
/
day02.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from typing import Mapping, Iterable, Tuple, Generator
from enum import Enum
example_rounds = [
'A Y',
'B X',
'C Z',
]
class Result(Enum):
LOSS = 1
DRAW = 2
WIN = 3
class Shape(Enum):
ROCK = 1
PAPER = 2
SCISSORS = 3
"""Mapping from characters to shapes"""
Shapemap = Mapping[str, Shape]
"""A Round is one move by "them" and one by "us"."""
Round = Tuple[Shape, Shape]
beats: Mapping[Shape, Shape] = {
Shape.ROCK: Shape.SCISSORS,
Shape.SCISSORS: Shape.PAPER,
Shape.PAPER: Shape.ROCK
}
result_score: Mapping[Result, int] = {
Result.WIN: 6,
Result.DRAW: 3,
Result.LOSS: 0,
}
shape_score: Mapping[Shape, int] = {
Shape.ROCK: 1,
Shape.PAPER: 2,
Shape.SCISSORS: 3,
}
def get_result(theirs: Shape, ours: Shape) -> Result:
if theirs == ours:
return Result.DRAW
elif beats[ours] == theirs:
return Result.WIN
else:
return Result.LOSS
def get_score(theirs: Shape, ours: Shape) -> int:
"""
>>> get_score(Shape.ROCK, Shape.PAPER)
8
"""
return shape_score[ours] + result_score[get_result(theirs, ours)]
def problem1(rounds: Iterable[Round]) -> int:
return sum(
get_score(theirs, ours)
for theirs, ours in rounds
)
def rounds_from_file(path, shapemap: Shapemap) -> Generator[Round, None, None]:
with open(path) as f:
for line in f.readlines():
theirs_char, ours_char = line.rstrip().split(' ')
theirs, ours = shapemap[theirs_char], shapemap[ours_char]
yield (theirs, ours)
if __name__ == '__main__':
char_to_shape: Shapemap = {
'A': Shape.ROCK,
'B': Shape.PAPER,
'C': Shape.SCISSORS,
'Y': Shape.PAPER,
'X': Shape.ROCK,
'Z': Shape.SCISSORS,
}
print('Problem 1:', problem1(rounds_from_file('./day02.txt', char_to_shape)))