-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.py
79 lines (56 loc) · 1.51 KB
/
day2.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
# vi: set shiftwidth=4 tabstop=4 expandtab:
import datetime
import os
top_dir = os.path.dirname(os.path.abspath(__file__)) + "/../../"
def get_guide_from_str(string):
return [tuple(l.strip().split(" ")) for l in string.splitlines()]
def get_guide_from_file(file_path=top_dir + "resources/year2022_day2_input.txt"):
with open(file_path) as f:
return get_guide_from_str(f.read())
SHAPE_SCORE = {
"A": 1, # Rock
"B": 2, # Paper
"C": 3, # Scissors
"X": 1, # Rock
"Y": 2, # Paper
"Z": 3, # Scissors
}
OUTCOME_SCORE = {
1: 0, # Loss
0: 3, # Draw
2: 6, # Win
}
def score1(p1, p2):
s1, s2 = SHAPE_SCORE[p1], SHAPE_SCORE[p2]
return s2 + OUTCOME_SCORE[(s1 - s2) % 3]
def part1(guide):
return sum(score1(*g) for g in guide)
def score2(p, outcome):
outcome_values = {
"X": 1, # Loss
"Y": 0, # Draw
"Z": 2, # Win
}
outcome_val = outcome_values[outcome]
s2 = (SHAPE_SCORE[p] - outcome_val - 1) % 3 + 1
return s2 + OUTCOME_SCORE[outcome_val]
def part2(guide):
return sum(score2(*g) for g in guide)
def run_tests():
guide = get_guide_from_str(
"""A Y
B X
C Z"""
)
assert part1(guide) == 15
assert part2(guide) == 12
def get_solutions():
guide = get_guide_from_file()
print(part1(guide) == 14264)
print(part2(guide) == 12382)
if __name__ == "__main__":
begin = datetime.datetime.now()
run_tests()
get_solutions()
end = datetime.datetime.now()
print(end - begin)