summaryrefslogtreecommitdiffstats
path: root/day7/__init__.py
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <tom@vanderlee.io>2021-12-07 16:06:04 +0100
committerGravatar Tom van der Lee <tom@vanderlee.io>2021-12-07 16:06:04 +0100
commitb2c17ba9c350dd52688c1eee910c801b794e2ea7 (patch)
tree55741ce98c60e851ac242d7a0eca8bd139cc9187 /day7/__init__.py
parent906fc128a0f99fec656901f55055ef78e10693b4 (diff)
download2021-b2c17ba9c350dd52688c1eee910c801b794e2ea7.tar.gz
2021-b2c17ba9c350dd52688c1eee910c801b794e2ea7.tar.bz2
2021-b2c17ba9c350dd52688c1eee910c801b794e2ea7.zip
Day 7: Slow
Diffstat (limited to 'day7/__init__.py')
-rw-r--r--day7/__init__.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/day7/__init__.py b/day7/__init__.py
new file mode 100644
index 0000000..b3579b7
--- /dev/null
+++ b/day7/__init__.py
@@ -0,0 +1,41 @@
1from collections import Counter
2from copy import copy
3from typing import List
4
5from aoc import BaseAssignment
6
7class Assignment(BaseAssignment):
8 def parse_item(self, item: str) -> List[int]:
9 return [ int(i) for i in item.split(',') ]
10
11 def read_input(self, example = False) -> List[int]:
12 return next(super().read_input(example))
13
14 @staticmethod
15 def calculate_cost(start, end) -> int:
16 raise NotImplementedError()
17
18 def run(self, input: List[int]) -> int:
19 return min([
20 sum([
21 self.calculate_cost(item, position)
22 for item in input
23 ])
24 for position in range(min(input), max(input) + 1)
25 ])
26
27class AssignmentOne(Assignment):
28 example_result = 37
29
30 @staticmethod
31 def calculate_cost(start, end) -> int:
32 return abs(end - start)
33
34class AssignmentTwo(Assignment):
35 example_result = 168
36
37 @staticmethod
38 def calculate_cost(start, end) -> int:
39 base_cost = AssignmentOne.calculate_cost(start, end)
40 extra = sum(i for i in range(base_cost))
41 return base_cost + extra \ No newline at end of file