diff options
| author | 2021-12-15 01:40:56 +0100 | |
|---|---|---|
| committer | 2021-12-15 10:33:53 +0100 | |
| commit | 10cca13d8d722bfbae749c285c380233079f65a2 (patch) | |
| tree | f31fe62477e25b84427af648d45e9dce5d07d0f5 | |
| parent | 5b4e79cc749bd2d6451dc0b1fb03f5b500eb8873 (diff) | |
| download | 2021-10cca13d8d722bfbae749c285c380233079f65a2.tar.gz 2021-10cca13d8d722bfbae749c285c380233079f65a2.tar.bz2 2021-10cca13d8d722bfbae749c285c380233079f65a2.zip | |
Day 14
| -rw-r--r-- | day14/__init__.py | 77 | ||||
| -rw-r--r-- | day14/example.txt | 18 | ||||
| -rw-r--r-- | day14/input.txt | 102 |
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 @@ | |||
| 1 | import math | ||
| 2 | from abc import ABC | ||
| 3 | from collections import Counter | ||
| 4 | from copy import copy | ||
| 5 | from typing import List, Tuple, Union, Dict | ||
| 6 | |||
| 7 | from aoc import BaseAssignment | ||
| 8 | |||
| 9 | |||
| 10 | class 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 | |||
| 70 | class AssignmentOne(Assignment): | ||
| 71 | example_result = 1588 | ||
| 72 | steps = 10 | ||
| 73 | |||
| 74 | class 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 @@ | |||
| 1 | NNCB | ||
| 2 | |||
| 3 | CH -> B | ||
| 4 | HH -> N | ||
| 5 | CB -> H | ||
| 6 | NH -> C | ||
| 7 | HB -> C | ||
| 8 | HC -> B | ||
| 9 | HN -> C | ||
| 10 | NN -> C | ||
| 11 | BH -> H | ||
| 12 | NC -> B | ||
| 13 | NB -> B | ||
| 14 | BN -> B | ||
| 15 | BB -> N | ||
| 16 | BC -> B | ||
| 17 | CC -> N | ||
| 18 | CN -> 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 @@ | |||
| 1 | COPBCNPOBKCCFFBSVHKO | ||
| 2 | |||
| 3 | NS -> H | ||
| 4 | FS -> O | ||
| 5 | PO -> C | ||
| 6 | NV -> N | ||
| 7 | CK -> B | ||
| 8 | FK -> N | ||
| 9 | PS -> C | ||
| 10 | OF -> F | ||
| 11 | KK -> F | ||
| 12 | PP -> S | ||
| 13 | VS -> K | ||
| 14 | VB -> V | ||
| 15 | BP -> P | ||
| 16 | BB -> K | ||
| 17 | BF -> C | ||
| 18 | NN -> V | ||
| 19 | NO -> F | ||
| 20 | SV -> C | ||
| 21 | OK -> N | ||
| 22 | PH -> P | ||
| 23 | KV -> B | ||
| 24 | PN -> O | ||
| 25 | FN -> V | ||
| 26 | SK -> V | ||
| 27 | VC -> K | ||
| 28 | BH -> P | ||
| 29 | BO -> S | ||
| 30 | HS -> H | ||
| 31 | HK -> S | ||
| 32 | HC -> S | ||
| 33 | HF -> B | ||
| 34 | PC -> C | ||
| 35 | CF -> B | ||
| 36 | KN -> H | ||
| 37 | CS -> N | ||
| 38 | SP -> O | ||
| 39 | VH -> N | ||
| 40 | CC -> K | ||
| 41 | KP -> N | ||
| 42 | NP -> C | ||
| 43 | FO -> H | ||
| 44 | FV -> N | ||
| 45 | NC -> F | ||
| 46 | KB -> N | ||
| 47 | VP -> O | ||
| 48 | KO -> F | ||
| 49 | CP -> F | ||
| 50 | OH -> F | ||
| 51 | KC -> H | ||
| 52 | NB -> F | ||
| 53 | HO -> P | ||
| 54 | SC -> N | ||
| 55 | FF -> B | ||
| 56 | PB -> H | ||
| 57 | FB -> K | ||
| 58 | SN -> B | ||
| 59 | VO -> K | ||
| 60 | OO -> N | ||
| 61 | NF -> B | ||
| 62 | ON -> P | ||
| 63 | SF -> H | ||
| 64 | FP -> H | ||
| 65 | HV -> B | ||
| 66 | NH -> B | ||
| 67 | CO -> C | ||
| 68 | PV -> P | ||
| 69 | VV -> K | ||
| 70 | KS -> P | ||
| 71 | OS -> S | ||
| 72 | SB -> P | ||
| 73 | OC -> N | ||
| 74 | SO -> K | ||
| 75 | BS -> B | ||
| 76 | CH -> V | ||
| 77 | PK -> F | ||
| 78 | OB -> P | ||
| 79 | CN -> N | ||
| 80 | CB -> N | ||
| 81 | VF -> O | ||
| 82 | VN -> K | ||
| 83 | PF -> P | ||
| 84 | SH -> H | ||
| 85 | FH -> N | ||
| 86 | HP -> P | ||
| 87 | KF -> V | ||
| 88 | BK -> H | ||
| 89 | OP -> C | ||
| 90 | HH -> F | ||
| 91 | SS -> V | ||
| 92 | BN -> C | ||
| 93 | OV -> F | ||
| 94 | HB -> P | ||
| 95 | FC -> C | ||
| 96 | BV -> H | ||
| 97 | VK -> S | ||
| 98 | NK -> K | ||
| 99 | CV -> K | ||
| 100 | HN -> K | ||
| 101 | BC -> K | ||
| 102 | KH -> P | ||
