summaryrefslogtreecommitdiffstats
path: root/day2/__init__.py
blob: e16787f919c80b222613cfebdb719d289054f980 (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
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):
    def run(self, input: List) -> int:
        new_input = [
            input[i - 2] + input[i - 1] + input[i]
            for i in range(2, len(input))
        ]

        return AssignmentOne(path='').run(new_input)