-
Notifications
You must be signed in to change notification settings - Fork 0
/
2022_2_scissors_jonny_2.py
73 lines (48 loc) · 1.16 KB
/
2022_2_scissors_jonny_2.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
68
69
70
71
72
73
from functools import cache
class Play:
@classmethod
def create(cls, key):
# Can we do something here to dynamically change the type at runtime?
return Play.source_keys[key]()
def wins(self, other):
if isinstance(other, self.beats):
return True
return False
class Rock(Play):
score = 1
@classmethod
@property
@cache
def beats(cls):
print("calculating beats for Rock")
return Scissors
class Paper(Play):
beats = Rock
score = 2
class Scissors(Play):
beats = Paper
score = 3
Play.source_keys = {
'X': Rock,
'A': Rock,
'Y': Paper,
'B': Paper,
'Z': Scissors,
'C': Scissors
}
def get_winner(me, opponent) -> int:
if me.wins(opponent):
return 6
elif opponent.wins(me):
return 0
return 3
def play(me, opponent) -> int:
return me.score + get_winner(me, opponent)
score = 0
with open('2022_2_scissors_input.txt') as fh:
line = fh.readline()
while line:
plays = line.strip().split(' ')
score += play(Play.create(plays[1]), Play.create(plays[0]))
line = fh.readline()
print(score)