-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.py
88 lines (75 loc) · 2.23 KB
/
day02.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from utils.solution_base import SolutionBase
import re
# A - X Rock => 1
# B - Y Paper => 2
# C - Z Scissors => 3
# Lose => 0 points
# Tie => 3 point
# Win => 6 points
class Solution(SolutionBase):
types_1 = {
"A": "rock",
"B": "paper",
"C": "scissors",
"X": "rock",
"Y": "paper",
"Z": "scissors",
}
types_2 = {
"X": "lose",
"Y": "draw",
"Z": "win",
}
points_hand = {"rock": 1, "paper": 2, "scissors": 3}
def wins(self, enemy, player):
if enemy == "rock" and player == "paper":
return 6
elif enemy == "paper" and player == "scissors":
return 6
elif enemy == "scissors" and player == "rock":
return 6
elif enemy == player:
return 3
else:
return 0
def strategy(self, enemy, strategy):
if strategy == "lose":
if enemy == "rock":
return "scissors"
elif enemy == "paper":
return "rock"
elif enemy == "scissors":
return "paper"
elif strategy == "draw":
if enemy == "rock":
return "rock"
elif enemy == "paper":
return "paper"
elif enemy == "scissors":
return "scissors"
elif strategy == "win":
if enemy == "rock":
return "paper"
elif enemy == "paper":
return "scissors"
elif enemy == "scissors":
return "rock"
def part1(self, data):
points = 0
for game in data:
[enemy, player] = game.split(" ")
enemy = self.types_1[enemy]
player = self.types_1[player]
points += self.wins(enemy, player)
points += self.points_hand[player]
return points
def part2(self, data):
points = 0
for game in data:
[enemy, strategy] = game.split(" ")
enemy = self.types_1[enemy]
strategy = self.types_2[strategy]
player = self.strategy(enemy, strategy)
points += self.wins(enemy, player)
points += self.points_hand[player]
return points