diff options
Diffstat (limited to 'day2/__init__.py')
| -rw-r--r-- | day2/__init__.py | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/day2/__init__.py b/day2/__init__.py new file mode 100644 index 0000000..19faab9 --- /dev/null +++ b/day2/__init__.py | |||
| @@ -0,0 +1,98 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | from abc import ABC | ||
| 3 | from enum import Enum | ||
| 4 | from typing import Iterator | ||
| 5 | |||
| 6 | from aoc import BaseAssignment | ||
| 7 | |||
| 8 | |||
| 9 | class TheirMove(Enum): | ||
| 10 | Rock = "A" | ||
| 11 | Paper = "B" | ||
| 12 | Scissors = "C" | ||
| 13 | |||
| 14 | |||
| 15 | class YourMove(Enum): | ||
| 16 | Rock = "X" | ||
| 17 | Paper = "Y" | ||
| 18 | Scissors = "Z" | ||
| 19 | |||
| 20 | |||
| 21 | class GameOutcome(Enum): | ||
| 22 | Lose = "X" | ||
| 23 | Draw = "Y" | ||
| 24 | Win = "Z" | ||
| 25 | |||
| 26 | |||
| 27 | class Assignment(BaseAssignment, ABC): | ||
| 28 | def calculate_score(self, opponent: TheirMove, you: YourMove) -> int: | ||
| 29 | game_result_score = { | ||
| 30 | TheirMove.Rock: { | ||
| 31 | YourMove.Rock: 3, | ||
| 32 | YourMove.Paper: 6, | ||
| 33 | YourMove.Scissors: 0, | ||
| 34 | }, | ||
| 35 | TheirMove.Paper: { | ||
| 36 | YourMove.Rock: 0, | ||
| 37 | YourMove.Paper: 3, | ||
| 38 | YourMove.Scissors: 6, | ||
| 39 | }, | ||
| 40 | TheirMove.Scissors: { | ||
| 41 | YourMove.Rock: 6, | ||
| 42 | YourMove.Paper: 0, | ||
| 43 | YourMove.Scissors: 3, | ||
| 44 | }, | ||
| 45 | } | ||
| 46 | |||
| 47 | you_thrown_score = { | ||
| 48 | YourMove.Rock: 1, | ||
| 49 | YourMove.Paper: 2, | ||
| 50 | YourMove.Scissors: 3, | ||
| 51 | } | ||
| 52 | |||
| 53 | return game_result_score[opponent][you] + you_thrown_score[you] | ||
| 54 | |||
| 55 | def play_game(self, col1: str, col2: str) -> int: | ||
| 56 | raise NotImplementedError() | ||
| 57 | |||
| 58 | def run(self, input: Iterator) -> int: | ||
| 59 | return sum([self.play_game(*game.split(" ")) for game in input]) | ||
| 60 | |||
| 61 | |||
| 62 | class AssignmentOne(Assignment): | ||
| 63 | example_result = 15 | ||
| 64 | |||
| 65 | def play_game(self, col1: str, col2: str) -> int: | ||
| 66 | return self.calculate_score(TheirMove(col1), YourMove(col2)) | ||
| 67 | |||
| 68 | |||
| 69 | class AssignmentTwo(Assignment): | ||
| 70 | example_result = 12 | ||
| 71 | |||
| 72 | def calculate_move( | ||
| 73 | self, opponent: TheirMove, game_outcome: GameOutcome | ||
| 74 | ) -> YourMove: | ||
| 75 | next_move = { | ||
| 76 | TheirMove.Rock: { | ||
| 77 | GameOutcome.Lose: YourMove.Scissors, | ||
| 78 | GameOutcome.Draw: YourMove.Rock, | ||
| 79 | GameOutcome.Win: YourMove.Paper, | ||
| 80 | }, | ||
| 81 | TheirMove.Paper: { | ||
| 82 | GameOutcome.Lose: YourMove.Rock, | ||
| 83 | GameOutcome.Draw: YourMove.Paper, | ||
| 84 | GameOutcome.Win: YourMove.Scissors, | ||
| 85 | }, | ||
| 86 | TheirMove.Scissors: { | ||
| 87 | GameOutcome.Lose: YourMove.Paper, | ||
| 88 | GameOutcome.Draw: YourMove.Scissors, | ||
| 89 | GameOutcome.Win: YourMove.Rock, | ||
| 90 | }, | ||
| 91 | } | ||
| 92 | |||
| 93 | return next_move[opponent][game_outcome] | ||
| 94 | |||
| 95 | def play_game(self, col1: str, col2: str) -> int: | ||
| 96 | return self.calculate_score( | ||
| 97 | TheirMove(col1), self.calculate_move(TheirMove(col1), GameOutcome(col2)) | ||
| 98 | ) | ||
