summaryrefslogtreecommitdiffstats
path: root/day2/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'day2/__init__.py')
-rw-r--r--day2/__init__.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/day2/__init__.py b/day2/__init__.py
new file mode 100644
index 0000000..e16787f
--- /dev/null
+++ b/day2/__init__.py
@@ -0,0 +1,44 @@
1from dataclasses import dataclass
2from typing import Iterator, List
3
4from aoc import BaseAssignment
5
6@dataclass
7class Instruction:
8 direction: str
9 steps: int
10
11class Assignment(BaseAssignment):
12 def parse_item(self, item: str) -> Instruction:
13 direction, steps = item.split(' ')
14 return Instruction(direction=direction, steps=int(steps))
15
16 def read_input(self, example = False) -> List[Instruction]:
17 return list(super().read_input(example))
18
19class AssignmentOne(Assignment):
20 depth = 0
21 horizontal = 0
22
23 def run(self, input: List[Instruction]) -> int:
24 for instruction in input:
25 match instruction.direction:
26 case 'forward':
27 self.horizontal += instruction.steps
28 case 'up':
29 self.depth -= instruction.steps
30 case 'down':
31 self.depth += instruction.steps
32
33 return self.depth * self.horizontal
34
35
36
37class AssignmentTwo(Assignment):
38 def run(self, input: List) -> int:
39 new_input = [
40 input[i - 2] + input[i - 1] + input[i]
41 for i in range(2, len(input))
42 ]
43
44 return AssignmentOne(path='').run(new_input)