summaryrefslogtreecommitdiffstats
path: root/day2/__init__.py
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <tom@vanderlee.io>2021-12-02 17:49:29 +0100
committerGravatar Tom van der Lee <tom@vanderlee.io>2021-12-02 17:49:29 +0100
commit824eaed0232ca337ff67f0cf0269f474e0471e2f (patch)
treeec8b04f8716b80d15353bfc9c9408a5f98affa85 /day2/__init__.py
parent4dec21f362c03136e9811a4f4c162fcd8c50544e (diff)
download2021-824eaed0232ca337ff67f0cf0269f474e0471e2f.tar.gz
2021-824eaed0232ca337ff67f0cf0269f474e0471e2f.tar.bz2
2021-824eaed0232ca337ff67f0cf0269f474e0471e2f.zip
Day2 part1
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)