summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--day14/__init__.py77
-rw-r--r--day14/example.txt18
-rw-r--r--day14/input.txt102
3 files changed, 197 insertions, 0 deletions
diff --git a/day14/__init__.py b/day14/__init__.py
new file mode 100644
index 0000000..738ea8f
--- /dev/null
+++ b/day14/__init__.py
@@ -0,0 +1,77 @@
1import math
2from abc import ABC
3from collections import Counter
4from copy import copy
5from typing import List, Tuple, Union, Dict
6
7from aoc import BaseAssignment
8
9
10class Assignment(BaseAssignment, ABC):
11 max_pre_calculations = 4
12 steps = 0
13 mapping_cache = dict()
14
15 def parse_item(self, item: str) -> Union[str, Tuple[str, str], None]:
16 if '>' in item:
17 return item.split(' -> ')
18
19 if item == '':
20 return None
21
22 return item
23
24
25 def read_input(self, example = False) -> Tuple[str, Dict[str, str]]:
26 input = super().read_input(example)
27
28 template = next(input)
29 next(input)
30
31 mapping = dict(input)
32
33 return template, mapping
34
35 def run(self, input: Tuple[str, Dict[str, str]]) -> int:
36 template, mapping = input
37
38 counter = Counter([
39 f'{template[i:i+2]}'
40 for i in range(len(template) - 1)
41 ])
42
43 char_count = {
44 **{
45 c: 1 if c in template else 0
46 for c
47 in mapping.values()
48 },
49 **Counter(template),
50 }
51
52 for _ in range(self.steps):
53 new_counter = {
54 pair: 0
55 for pair
56 in mapping.keys()
57 }
58
59 for key, value in counter.items():
60 middle = mapping[key]
61
62 char_count[middle] += value
63 new_counter[f'{key[0]}{middle}'] += value
64 new_counter[f'{middle}{key[1]}'] += value
65
66 counter = new_counter
67
68 return max(char_count.values()) - min(char_count.values())
69
70class AssignmentOne(Assignment):
71 example_result = 1588
72 steps = 10
73
74class AssignmentTwo(Assignment):
75 max_pre_calculations = 4
76 example_result = 2188189693529
77 steps = 40
diff --git a/day14/example.txt b/day14/example.txt
new file mode 100644
index 0000000..b5594dd
--- /dev/null
+++ b/day14/example.txt
@@ -0,0 +1,18 @@
1NNCB
2
3CH -> B
4HH -> N
5CB -> H
6NH -> C
7HB -> C
8HC -> B
9HN -> C
10NN -> C
11BH -> H
12NC -> B
13NB -> B
14BN -> B
15BB -> N
16BC -> B
17CC -> N
18CN -> C
diff --git a/day14/input.txt b/day14/input.txt
new file mode 100644
index 0000000..171af3e
--- /dev/null
+++ b/day14/input.txt
@@ -0,0 +1,102 @@
1COPBCNPOBKCCFFBSVHKO
2
3NS -> H
4FS -> O
5PO -> C
6NV -> N
7CK -> B
8FK -> N
9PS -> C
10OF -> F
11KK -> F
12PP -> S
13VS -> K
14VB -> V
15BP -> P
16BB -> K
17BF -> C
18NN -> V
19NO -> F
20SV -> C
21OK -> N
22PH -> P
23KV -> B
24PN -> O
25FN -> V
26SK -> V
27VC -> K
28BH -> P
29BO -> S
30HS -> H
31HK -> S
32HC -> S
33HF -> B
34PC -> C
35CF -> B
36KN -> H
37CS -> N
38SP -> O
39VH -> N
40CC -> K
41KP -> N
42NP -> C
43FO -> H
44FV -> N
45NC -> F
46KB -> N
47VP -> O
48KO -> F
49CP -> F
50OH -> F
51KC -> H
52NB -> F
53HO -> P
54SC -> N
55FF -> B
56PB -> H
57FB -> K
58SN -> B
59VO -> K
60OO -> N
61NF -> B
62ON -> P
63SF -> H
64FP -> H
65HV -> B
66NH -> B
67CO -> C
68PV -> P
69VV -> K
70KS -> P
71OS -> S
72SB -> P
73OC -> N
74SO -> K
75BS -> B
76CH -> V
77PK -> F
78OB -> P
79CN -> N
80CB -> N
81VF -> O
82VN -> K
83PF -> P
84SH -> H
85FH -> N
86HP -> P
87KF -> V
88BK -> H
89OP -> C
90HH -> F
91SS -> V
92BN -> C
93OV -> F
94HB -> P
95FC -> C
96BV -> H
97VK -> S
98NK -> K
99CV -> K
100HN -> K
101BC -> K
102KH -> P