-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02.py
More file actions
62 lines (53 loc) · 1.92 KB
/
02.py
File metadata and controls
62 lines (53 loc) · 1.92 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
class RockPaperScissors:
my_score = 0
def play_1(self, lines: list[str]):
for line in lines:
elf_and_mine = line.rstrip().split(' ')
elf = ord(elf_and_mine[0]) - ord('A') + 1
mine = ord(elf_and_mine[1]) - ord('X') + 1
outcome = self.compute_outcome(elf, mine)
self.my_score += outcome + mine
print(f'score = {self.my_score}')
def play_2(self, lines: list[str]):
for line in lines:
elf_and_strategy = line.rstrip().split(' ')
elf = ord(elf_and_strategy[0]) - ord('A') + 1
mine = self.from_strategy_2(elf, elf_and_strategy[1])
outcome = self.compute_outcome(elf, mine)
self.my_score += outcome + mine
print(f'score = {self.my_score}')
@staticmethod
def compute_outcome(elf, mine) -> int:
outcome = 0
if elf == mine:
outcome = 3
if mine == 2 and elf == 1 or \
mine == 3 and elf == 2 or \
mine == 1 and elf == 3:
outcome = 6
return outcome
@staticmethod
def from_strategy_2(elf, strategy) -> int:
# Default is draw
mine = elf
if strategy == 'X': # loose
mine = elf - 1
if elf == 1:
mine = 3
if strategy == 'Z': # win
mine = elf + 1
if elf == 3:
mine = 1
return mine
if __name__ == '__main__':
for input_dir in ['test-files', 'input-files']:
with open(f'{input_dir}/02.txt', mode='r', encoding='utf-8') as f:
game = RockPaperScissors()
game.play_1(f.readlines())
if input_dir == 'test-files':
assert game.my_score == 15
game = RockPaperScissors()
f.seek(0)
game.play_2(f.readlines())
if input_dir == 'test-files':
assert game.my_score == 12