-
Notifications
You must be signed in to change notification settings - Fork 0
/
2022_2_scissors.py
67 lines (53 loc) · 1.59 KB
/
2022_2_scissors.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
from functools import total_ordering
@total_ordering
class RockPaperScissors:
hand_shape_mapping = {
'A': 'Rock',
'B': 'Paper',
'C': 'Scissors',
'X': 'Rock',
'Y': 'Paper',
'Z': 'Scissors',
}
score_mapping = {
'Rock': 1,
'Paper': 2,
'Scissors': 3
}
def __init__(self, hand_shape):
self.hand_shape = hand_shape
self.score = self.score_mapping[self.hand_shape]
def __eq__(self, other):
return self.score == other.score
def __gt__(self, other):
return (self.score - other.score) % 3 == 1
@classmethod
def from_letter(cls, letter):
hand_shape = cls.hand_shape_mapping[letter]
return cls(hand_shape)
result_mapping = {
'X': 'lose',
'Y': 'draw',
'Z': 'win',
}
splitlines = (line.strip().split()
for line in open("2022_2_scissors_input.txt"))
rounds = ((RockPaperScissors.from_letter(them), result_mapping[result])
for them, result in splitlines)
rock = RockPaperScissors('Rock')
paper = RockPaperScissors('Paper')
scissors = RockPaperScissors('Scissors')
options = (rock, paper, scissors)
total_score = 0
for them, result in rounds:
if result == 'win':
total_score += 6
me = next(option for option in options if option > them)
elif result == 'draw':
total_score += 3
me = next(option for option in options if option == them)
elif result == 'lose':
total_score += 0
me = next(option for option in options if option < them)
total_score += me.score
print(total_score)