-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2.py
More file actions
62 lines (47 loc) · 1.5 KB
/
day2.py
File metadata and controls
62 lines (47 loc) · 1.5 KB
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
# Parse list of games to a list of pairs
def read_input():
with open("inputs/day2.txt", "r") as fin:
return [l.strip().split() for l in fin]
# Aux function: given a game, get the score
def get_score(game):
WINS = [['A', 'Y'], ['B', 'Z'], ['C', 'X']]
DRAWS = [['A', 'X'], ['B', 'Y'], ['C', 'Z']]
score = 0
# Score depending on win or draw (losing gives you no points)
if game in WINS:
score += 6
elif game in DRAWS:
score += 3
# Score depending on your output (1 for rock, 2 for scissors, 3 for paper)
if game[1] == 'X':
score += 1
elif game[1] == 'Y':
score += 2
elif game[1] == 'Z':
score += 3
return score
# Auxiliar function: given opponent move and game result, obtain your move
def get_move(game):
if game in [['A', 'X'], ['B', 'Z'], ['C', 'Y']]:
return 'Z'
elif game in [['A', 'Y'], ['B', 'X'], ['C', 'Z']]:
return 'X'
elif game in [['A', 'Z'], ['B', 'Y'], ['C', 'X']]:
return 'Y'
# PROBLEM 1: Score of games, assuming second column is your choice
def problem_1():
matches = read_input()
score = 0
for game in matches:
score += get_score(game)
return score
# PROBLEM 2: Score of games, assuming second column is result of the game
def problem_2():
matches = read_input()
score = 0
for game in matches:
score += get_score([game[0], get_move(game)])
return score
if __name__ == '__main__':
print(problem_1())
print(problem_2())