-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.py
56 lines (41 loc) · 1.34 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
import collections
WIN_SCORE, DRAW_SCORE, LOOSE_SCORE = 6, 3, 0
Move = collections.namedtuple('Move',['points','beats'])
ROCK = Move(points=1, beats=[])
PAPER = Move(points=2, beats=[ROCK])
SCISSOR = Move(points=3, beats=[PAPER])
ROCK.beats.append(SCISSOR)
GUIDE_TO_MOVE = {
'A': ROCK,
'B': PAPER,
'C': SCISSOR,
'X': ROCK,
'Y': PAPER,
'Z': SCISSOR,
}
ALL_MOVES = [ROCK, PAPER, SCISSOR]
GUIDE_TO_RESULT = {
'X': (LOOSE_SCORE, lambda move: move.beats[0]),
'Y': (DRAW_SCORE, lambda move: move),
'Z': (WIN_SCORE, lambda move: next(filter(lambda x: move != x and x not in move.beats, ALL_MOVES)))
}
input_file = open('input-2-1.txt')
total_score_sol1 = 0
total_score_sol2 = 0
for line in input_file:
plays = line.strip().split(' ')
opponent_move = GUIDE_TO_MOVE[plays[0]]
column2 =plays[1]
my_move = GUIDE_TO_MOVE[column2]
if opponent_move in my_move.beats:
total_score_sol1 += WIN_SCORE
elif my_move == opponent_move:
total_score_sol1 += DRAW_SCORE
total_score_sol1 += my_move.points
guide_to_result = GUIDE_TO_RESULT[column2]
my_potential_move = guide_to_result[1](opponent_move)
total_score_sol2 += guide_to_result[0] + my_potential_move.points
# Solution 1
print(f"score on following strategy guide as moves {total_score_sol1}")
# Solution 2
print(f"score on following strategy guide as results {total_score_sol2}")