-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.py
28 lines (22 loc) · 866 Bytes
/
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
with open('day2.in') as file:
strategy = [line.strip().split() for line in file]
ROCK, PAPER, SCISSORS = 0, 1, 2
DRAW, WIN, LOSE = 0, 1, 2
def points(my_hand, outcome):
points_hand = {ROCK: 1, PAPER: 2, SCISSORS: 3}
points_outcome = {LOSE: 0, DRAW: 3, WIN: 6}
return points_hand[my_hand] + points_outcome[outcome]
def points1(input_a, input_b):
other_hand = 'ABC'.index(input_a)
my_hand = 'XYZ'.index(input_b)
outcome = (my_hand - other_hand) % 3
return points(my_hand, outcome)
def points2(input_a, input_b):
other_hand = 'ABC'.index(input_a)
outcome = {'X': LOSE, 'Y': DRAW, 'Z': WIN}[input_b]
my_hand = (other_hand + outcome) % 3
return points(my_hand, outcome)
part1 = sum(points1(*pair) for pair in strategy)
part2 = sum(points2(*pair) for pair in strategy)
print('Part 1:', part1)
print('Part 2:', part2)