blob: d99a0bebe4759dd0c0dbc3247d1c44f3c3802011 (
plain)
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
|
from dataclasses import dataclass
from typing import Iterator, List
from aoc import BaseAssignment
@dataclass
class Instruction:
direction: str
steps: int
class Assignment(BaseAssignment):
def parse_item(self, item: str) -> Instruction:
direction, steps = item.split(' ')
return Instruction(direction=direction, steps=int(steps))
def read_input(self, example = False) -> List[Instruction]:
return list(super().read_input(example))
class AssignmentOne(Assignment):
example_result = 150
depth = 0
horizontal = 0
def run(self, input: List[Instruction]) -> int:
for instruction in input:
match instruction.direction:
case 'forward':
self.horizontal += instruction.steps
case 'up':
self.depth -= instruction.steps
case 'down':
self.depth += instruction.steps
return self.depth * self.horizontal
class AssignmentTwo(Assignment):
example_result = 900
aim = 0
depth = 0
horizontal = 0
def run(self, input: List) -> int:
for instruction in input:
match instruction.direction:
case 'forward':
self.horizontal += instruction.steps
self.depth += instruction.steps * self.aim
case 'up':
self.aim -= instruction.steps
case 'down':
self.aim += instruction.steps
return self.depth * self.horizontal
|