-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_2.py
More file actions
94 lines (81 loc) · 2.5 KB
/
day_2.py
File metadata and controls
94 lines (81 loc) · 2.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
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
"""
Rock defeats Scissors, Scissors defeats Paper, and Paper defeats Rock.
What would your total score be if everything goes exactly according to your strategy guide?
"""
player_choice = {
'A': 1, #rock
'X': 1, #rock
'B': 2, #paper
'Y': 2, #paper
'C': 3, #scissors
'Z': 3, #scissors
}
game_rules = {
'X': 'C', # Rock defeats Scissors
'Y': 'A', # Paper defeats Rock
'Z': 'B' # Scissors defeats Paper
}
round_score = {
'lost': 0,
'draw': 3,
'won': 6
}
with open('day_2_input.txt', encoding='utf-8') as input_data:
game_pairs = input_data.readlines()
game_pairs = [pair.replace('\n','') for pair in game_pairs]
# Part 1
game_score = 0
for i in range(len(game_pairs)):
player_1_choice = game_pairs[i][0]
player_2_choice = game_pairs[i][2]
player_1_score = player_choice[game_pairs[i][0]]
player_2_score = player_choice[game_pairs[i][2]]
if player_1_score == player_2_score:
score = player_2_score + round_score['draw']
game_score = game_score + score
elif game_rules[player_2_choice] == player_1_choice:
score = player_2_score + round_score['won']
game_score = game_score + score
else:
score = player_2_score
game_score = game_score + score
print(game_score)
# Part 2
# new input re the rules
player_choice = {
'A': 1, #rock
'B': 2, #paper
'C': 3, #scissors
}
game_rules = {
'A': 'C', # Rock defeats Scissors
'B': 'A', # Paper defeats Rock
'C': 'B' # Scissors defeats Paper
}
game_rules_2 = {
'X': 'lost',
'Y': 'draw',
'Z': 'won'
}
game_score = 0
for i in range(len(game_pairs)):
player_1_choice = game_pairs[i][0]
round_result = game_rules_2[game_pairs[i][2]]
player_1_score = player_choice[player_1_choice]
if round_result == 'draw':
player_2_choice = player_1_choice
player_2_score = player_1_score
score = player_2_score + round_score['draw']
game_score = score + game_score
elif round_result == 'lost':
player_2_choice = game_rules[player_1_choice]
player_2_score = player_choice[player_2_choice]
score = player_2_score + round_score['lost']
game_score = score + game_score
else:
game_rules_reversed = dict((value, key) for key, value in game_rules.items())
player_2_choice = game_rules_reversed[player_1_choice]
player_2_score = player_choice[player_2_choice]
score = player_2_score + round_score['won']
game_score = score + game_score
print(game_score)