summaryrefslogtreecommitdiffstats
path: root/day10
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <tom@ScaryTerry.local>2020-12-18 00:49:00 +0100
committerGravatar Tom van der Lee <tom@ScaryTerry.local>2020-12-18 00:49:00 +0100
commit6c18350a39ddb701fe34676a84b91dec0848160a (patch)
treef3d7da4dd2b979d1a9855a48a883ed2d052dc249 /day10
parent37aa8eec0498d7e8491084711132f16db9129a39 (diff)
download2020-master.tar.gz
2020-master.tar.bz2
2020-master.zip
Added day 10HEADmaster
Diffstat (limited to 'day10')
-rw-r--r--day10/__init__.py74
-rw-r--r--day10/example.txt31
-rw-r--r--day10/input.txt90
3 files changed, 195 insertions, 0 deletions
diff --git a/day10/__init__.py b/day10/__init__.py
new file mode 100644
index 0000000..62cc5be
--- /dev/null
+++ b/day10/__init__.py
@@ -0,0 +1,74 @@
1from dataclasses import dataclass
2from functools import lru_cache, reduce
3from typing import List, Generator, Dict, Optional
4
5from aoc import BaseAssignment
6
7
8class Assignment(BaseAssignment):
9 def parse_item(self, item: str) -> int:
10 return int(item)
11
12
13class AssignmentOne(Assignment):
14 def run(self, input: Generator) -> int:
15 adapters = sorted(input)
16 device_adapter = max(adapters) + 3
17
18 adapter_list = [
19 0, *adapters, device_adapter
20 ]
21
22 differences = [
23 adapter_list[index + 1] - adapter
24 for index, adapter in enumerate(adapter_list)
25 if adapter != device_adapter
26 ]
27
28 return differences.count(1) * differences.count(3)
29
30
31@dataclass
32class Node:
33 value: int
34 next: List['Node']
35 paths: int = 0
36
37 def __repr__(self):
38 return str(f'<Node: {self.value}, paths: {self.paths}>')
39
40 def __post_init__(self):
41 self.paths = reduce(lambda count, node: count + 1 if len(node.next) == 0 else count + node.paths, self.next, 0)
42
43
44class AssignmentTwo(Assignment):
45 def generate_graph(
46 self, input: List, pointers: Dict[int, Node] = None) -> Optional[Node]:
47 if pointers is None:
48 pointers = {}
49
50 if len(input) == 0:
51 return
52
53 value, next = input[0], input[1:]
54
55 pointers[value] = Node(value=value, next=[
56 (
57 pointers[next_value]
58 if next_value in pointers
59 else self.generate_graph(
60 input=input[index + 1:],
61 pointers=pointers
62 )
63 )
64 for index, next_value in enumerate(next[:3])
65 if next_value - value <= 3
66 ])
67
68 return pointers[value]
69
70 def run(self, input: Generator) -> int:
71 adapters = sorted(input)
72 device_adapter = max(adapters) + 3
73
74 return self.generate_graph([0, *adapters, device_adapter]).paths
diff --git a/day10/example.txt b/day10/example.txt
new file mode 100644
index 0000000..be5c492
--- /dev/null
+++ b/day10/example.txt
@@ -0,0 +1,31 @@
128
233
318
442
531
614
746
820
948
1047
1124
1223
1349
1445
1519
1638
1739
1811
191
2032
2125
2235
238
2417
257
269
274
282
2934
3010
313 \ No newline at end of file
diff --git a/day10/input.txt b/day10/input.txt
new file mode 100644
index 0000000..94a2db6
--- /dev/null
+++ b/day10/input.txt
@@ -0,0 +1,90 @@
1114
251
3122
426
5121
690
720
8113
98
10138
1157
1244
13135
1476
15134
1615
1721
18119
1952
20118
21107
2299
2373
2472
25106
2641
27129
2883
2919
3066
31132
3256
3332
3479
3527
36115
37112
3858
39102
4064
4150
422
4339
443
4577
4685
47103
48140
4928
50133
5178
5234
5313
5461
5525
5635
5789
5840
597
6024
6133
6296
63108
6471
6511
66128
6792
68111
6955
7080
7191
7231
7370
74101
7514
7618
7712
784
7984
80125
81120
82100
8365
8486
8593
8667
87139
881
8947
9038 \ No newline at end of file