-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.py
112 lines (94 loc) · 2.5 KB
/
2.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# --- Day 2: Rock Paper Scissors ---
# I need to calculate the scores of a series of RPS rounds
# Opponent:
# A: Rock
# B: Paper
# C: Scissors
# Me:
# X: Rock
# Y: Paper
# Z: Scissors
# Points:
# Rock: 1
# Paper: 2
# Scissors: 3
# Lose: 0
# Draw: 3
# Win: 6
# Open file
f = open("2_input.txt", "r")
# Sum the scores of each line (RPS round)
total_score = 0
for line in f:
# Extract shapes
opponent_shape = line[0]
my_shape = line[2]
# Calculate shape points
match my_shape:
case "X":
total_score += 1
case "Y":
total_score += 2
case "Z":
total_score += 3
# Calculate win points
if (opponent_shape+my_shape) == "AX" or (opponent_shape+my_shape) == "BY" or (opponent_shape+my_shape) == "CZ":
# Draw
total_score += 3
elif (opponent_shape+my_shape) == "AY" or (opponent_shape+my_shape) == "BZ" or (opponent_shape+my_shape) == "CX":
# Win
total_score += 6
print("Part 1 total score:", total_score)
# --- Part Two ---
# Strategy:
# X: Lose
# Y: Draw
# Z: Win
# Open file
f = open("2_input.txt", "r")
# Sum the scores of each line (RPS round)
total_score = 0
for line in f:
# Extract shapes
opponent_shape = line[0]
strategy = line[2]
# Calculate points
if strategy == "Y":
# Draw
total_score += 3
match opponent_shape:
case "A":
# Opponent played Rock
total_score += 1
case "B":
# Opponent played Paper
total_score += 2
case "C":
# Opponent played Scissors
total_score += 3
elif strategy == "Z":
# Win
total_score += 6
match opponent_shape:
case "A":
# Opponent played Rock
total_score += 2
case "B":
# Opponent played Paper
total_score += 3
case "C":
# Opponent played Scissors
total_score += 1
else:
# Lose
match opponent_shape:
case "A":
# Opponent played Rock
total_score += 3
case "B":
# Opponent played Paper
total_score += 1
case "C":
# Opponent played Scissors
total_score += 2
print("Part 2 total score:", total_score)