Repository navigation
Expand file tree
/
Copy path2.py
More file actions
92 lines (68 loc) · 2.35 KB
/
Copy path2.py
File metadata and controls
92 lines (68 loc) · 2.35 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
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
89
90
91
92
import utils
DAY = 2
TITLE = 'Day 2: Rock Paper Scissors'
class RPS:
OPPONENT_ROCK = 'A'
OPPONENT_PAPER = 'B'
OPPONENT_SCISSORS = 'C'
# part 1
PICK_ROCK = 'X'
PICK_PAPER = 'Y'
PICK_SCISSORS = 'Z'
# part 2
RESULT_WIN = 'Z'
RESULT_DRAW = 'Y'
RESULT_LOSE = 'X'
def round_score1(round: str) -> int:
opponent, me = round.split(' ')
score = 0
score += shape_score(me)
score += outcome_score(opponent, me)
return score
def round_score2(round: str) -> int:
opponent, result = round.split(' ')
me = me_choose(opponent, result)
score = 0
score += shape_score(me)
score += outcome_score(opponent, me)
return score
def shape_score(shape: str):
match shape:
case RPS.PICK_ROCK:
return 1
case RPS.PICK_PAPER:
return 2
case RPS.PICK_SCISSORS:
return 3
case _:
return 0
def outcome_score(opponent: str, me: str):
match (opponent, me):
case (RPS.OPPONENT_ROCK, RPS.PICK_PAPER) | (RPS.OPPONENT_PAPER, RPS.PICK_SCISSORS) | (RPS.OPPONENT_SCISSORS, RPS.PICK_ROCK):
return 6
case (RPS.OPPONENT_ROCK, RPS.PICK_ROCK) | (RPS.OPPONENT_PAPER, RPS.PICK_PAPER) | (RPS.OPPONENT_SCISSORS, RPS.PICK_SCISSORS):
return 3
case _:
return 0
def me_choose(opponent: str, result: str):
match (opponent, result):
case (RPS.OPPONENT_ROCK, RPS.RESULT_DRAW) | (RPS.OPPONENT_PAPER, RPS.RESULT_LOSE) | (RPS.OPPONENT_SCISSORS, RPS.RESULT_WIN):
return RPS.PICK_ROCK
case (RPS.OPPONENT_ROCK, RPS.RESULT_WIN) | (RPS.OPPONENT_PAPER, RPS.RESULT_DRAW) | (RPS.OPPONENT_SCISSORS, RPS.RESULT_LOSE):
return RPS.PICK_PAPER
case (RPS.OPPONENT_ROCK, RPS.RESULT_LOSE) | (RPS.OPPONENT_PAPER, RPS.RESULT_WIN) | (RPS.OPPONENT_SCISSORS, RPS.RESULT_DRAW):
return RPS.PICK_SCISSORS
def procress_input(input: list[str]):
game_score1 = sum(map(round_score1, input))
game_score2 = sum(map(round_score2, input))
return (game_score1, game_score2)
def print_solutions(data: tuple[int, int]):
utils.print_sol_part(1, data[0])
utils.print_sol_part(2, data[1])
def main():
utils.print_title(TITLE)
lines = utils.read_input(DAY)
data = procress_input(lines)
print_solutions(data)
if __name__ == '__main__':
main()