-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.py
86 lines (75 loc) · 2.14 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
80
81
82
83
84
85
86
import os
datapath = os.path.join("data", "day2.txt")
def get_score_pick(your_pick):
score = 0
if your_pick == "X":
score += 1
elif your_pick == "Y":
score += 2
elif your_pick == "Z":
score += 3
return score
def get_score_match(opponent_pick, your_pick):
# If equal
if ord(opponent_pick) == ord(your_pick)-23:
return 3
if your_pick == "X":
if opponent_pick == "B":
return 0
elif opponent_pick == "C":
return 6
if your_pick == "Y":
if opponent_pick == "A":
return 6
elif opponent_pick == "C":
return 0
if your_pick == "Z":
if opponent_pick == "A":
return 0
elif opponent_pick == "B":
return 6
def decode_part2(opponent_pick, your_pick):
if your_pick == "X":
if opponent_pick == "A":
return "Z"
elif opponent_pick == "B":
return "X"
elif opponent_pick == "C":
return "Y"
if your_pick == "Y":
if opponent_pick == "A":
return "X"
elif opponent_pick == "B":
return "Y"
elif opponent_pick == "C":
return "Z"
if your_pick == "Z":
if opponent_pick == "A":
return "Y"
elif opponent_pick == "B":
return "Z"
elif opponent_pick == "C":
return "X"
def get_score_part1(opponent_pick, your_pick):
score = get_score_match(opponent_pick, your_pick) + get_score_pick(your_pick)
return score
def get_score_part2(opponent_pick, your_pick):
your_pick = decode_part2(opponent_pick, your_pick)
score = get_score_match(opponent_pick, your_pick) + get_score_pick(your_pick)
return score
## Part 1
with open(datapath) as f:
lines = f.readlines()
lines = [(x.strip().split(" ")) for x in lines]
# total_score = 0
# for i,j in lines:
# total_score += get_score_part1(i,j)
# print(total_score)
## Part 2
with open(datapath) as f:
lines = f.readlines()
lines = [(x.strip().split(" ")) for x in lines]
total_score = 0
for i,j in lines:
total_score += get_score_part2(i,j)
print(total_score)