summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--day2/__init__.py98
-rw-r--r--day2/example.txt3
-rw-r--r--day2/input.txt2500
3 files changed, 2601 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 -*-
2from abc import ABC
3from enum import Enum
4from typing import Iterator
5
6from aoc import BaseAssignment
7
8
9class TheirMove(Enum):
10 Rock = "A"
11 Paper = "B"
12 Scissors = "C"
13
14
15class YourMove(Enum):
16 Rock = "X"
17 Paper = "Y"
18 Scissors = "Z"
19
20
21class GameOutcome(Enum):
22 Lose = "X"
23 Draw = "Y"
24 Win = "Z"
25
26
27class 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
62class 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
69class 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 )
diff --git a/day2/example.txt b/day2/example.txt
new file mode 100644
index 0000000..db60e36
--- /dev/null
+++ b/day2/example.txt
@@ -0,0 +1,3 @@
1A Y
2B X
3C Z
diff --git a/day2/input.txt b/day2/input.txt
new file mode 100644
index 0000000..c4b1829
--- /dev/null
+++ b/day2/input.txt
@@ -0,0 +1,2500 @@
1C X
2C Y
3C X
4B X
5B Z
6A Z
7C Y
8C Z
9B Z
10C X
11B Y
12C Y
13C Y
14A Y
15C Y
16C Y
17C Z
18C X
19B Z
20C Y
21A Y
22A Y
23C Z
24B Y
25A Y
26C Z
27C Y
28A Y
29A Y
30B Y
31C Y
32C Z
33C Y
34B X
35B Z
36C Y
37B Z
38A X
39C Z
40A Y
41B Y
42C Y
43C Y
44B Z
45B Y
46A Z
47C X
48C X
49C Y
50C X
51B Z
52A Y
53B X
54B Z
55C Z
56C X
57C X
58B Z
59A Y
60B Y
61C Y
62C Y
63A Y
64C X
65A Y
66B Z
67C Y
68C Y
69B Y
70C Y
71A Z
72A Z
73B X
74A Y
75C Y
76A Y
77C Y
78C Y
79C X
80C Y
81B Z
82C Y
83C Z
84C X
85B X
86C Y
87C Y
88C X
89C Z
90A Y
91C X
92B Z
93C X
94A Y
95B Y
96C Y
97A Y
98A Y
99A Y
100B Y
101C Y
102A Y
103A Y
104C Z
105C Y
106B Y
107C X
108C Y
109B Z
110B Z
111C X
112C Y
113C X
114C Y
115C Y
116A Y
117C Y
118C X
119C Y
120C Y
121B Y
122B Z
123A Y
124B Y
125A Y
126A Y
127C Y
128B X
129C Y
130C X
131A Y
132C X
133C X
134C Y
135C Y
136B Z
137C X
138A Y
139B Z
140C Z
141C X
142A Y
143B Z
144C Y
145B Y
146A Y
147A Y
148C Y
149B Y
150C Y
151C X
152C Y
153A Y
154C Y
155A Y
156B Y
157C X
158C Y
159C Y
160B Z
161B Z