-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.py
executable file
·66 lines (50 loc) · 1.82 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
#! /usr/bin/env python3
### local imports
import utils
# Losing moves (+0 modifier)
losingMoves: dict[str, int] = {
"A Z": 3 + 0, # rock v scissors
"B X": 1 + 0, # paper v rock
"C Y": 2 + 0, # scissors v paper
}
# Draw moves (+3 modifier)
drawMoves: dict[str, int] = {
"A X": 1 + 3, # rock v rock
"B Y": 2 + 3, # paper v paper
"C Z": 3 + 3, # scissors v scissors
}
# Winning moves (+6 modifier)
winningMoves: dict[str, int] = {
"A Y": 2 + 6, # , rock v paper
"B Z": 3 + 6, # paper v scissors
"C X": 1 + 6, # scissors v rock
}
# Mapping of ALL the possible move combinations
allMoves: dict[str, int] = {**losingMoves, **drawMoves, **winningMoves}
@utils.part1
def part1(puzzleInput: str):
# Break the puzzle input down into a list of moves
moves = puzzleInput.strip().split("\n")
# Calculate the final score from the sum of point values of all moves in the list
utils.printAnswer(sum([allMoves[move] for move in moves]))
# Return the moves list for part 2 to work with
return moves
@utils.part2
def part2(_, moves: list[str]):
finalScore: int = 0
# Iterate through each move in the list
for opponentMove, outcome in [move.split(" ") for move in moves]:
# Select the list of possible moves based on the ordained outcome
possibleMoves = {"X": losingMoves, "Y": drawMoves, "Z": winningMoves}[
outcome
]
# Iterate through the possible moves to find one that matches the opponent's move.
# When it matches, add the point value and move on
for move, moveValue in possibleMoves.items():
if move.startswith(opponentMove):
finalScore += moveValue
break
# The answer is the final score
utils.printAnswer(finalScore)
if __name__ == "__main__":
utils.start()