summaryrefslogtreecommitdiffstats
path: root/day7/__init__.py
blob: 933b9e002186a18408de2ccad9e27b6264f4042c (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
from collections import Counter
from copy import copy
from typing import List

from aoc import BaseAssignment

class Assignment(BaseAssignment):
    def parse_item(self, item: str) -> List[int]:
        return [ int(i) for i in item.split(',') ]

    def read_input(self, example = False) -> List[int]:
        return next(super().read_input(example))

    @staticmethod
    def calculate_cost(start, end) -> int:
        raise NotImplementedError()

    def run(self, input: List[int]) -> int:
        return min([
            sum([
                self.calculate_cost(item, position)
                for item in input
            ])
            for position in range(min(input), max(input) + 1)
        ])

class AssignmentOne(Assignment):
    example_result = 37

    @staticmethod
    def calculate_cost(start, end) -> int:
        return abs(end - start)

class AssignmentTwo(Assignment):
    example_result = 168

    @staticmethod
    def calculate_cost(start, end) -> int:
        base_cost = AssignmentOne.calculate_cost(start, end)
        # extra = sum(i for i in range(base_cost))
        # return base_cost + extra
        return int(base_cost * (base_cost + 1) / 2)