summaryrefslogtreecommitdiffstats
path: root/day2/__init__.py
blob: 14229a9dea46c0cc914ff14e3da5757b918e993f (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
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):
    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):
    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