From 10cca13d8d722bfbae749c285c380233079f65a2 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Wed, 15 Dec 2021 01:40:56 +0100 Subject: Day 14 --- day14/__init__.py | 77 +++++++++++++++++++++++++++++++++++++++++ day14/example.txt | 18 ++++++++++ day14/input.txt | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 day14/__init__.py create mode 100644 day14/example.txt create mode 100644 day14/input.txt (limited to 'day14') 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 @@ +import math +from abc import ABC +from collections import Counter +from copy import copy +from typing import List, Tuple, Union, Dict + +from aoc import BaseAssignment + + +class Assignment(BaseAssignment, ABC): + max_pre_calculations = 4 + steps = 0 + mapping_cache = dict() + + def parse_item(self, item: str) -> Union[str, Tuple[str, str], None]: + if '>' in item: + return item.split(' -> ') + + if item == '': + return None + + return item + + + def read_input(self, example = False) -> Tuple[str, Dict[str, str]]: + input = super().read_input(example) + + template = next(input) + next(input) + + mapping = dict(input) + + return template, mapping + + def run(self, input: Tuple[str, Dict[str, str]]) -> int: + template, mapping = input + + counter = Counter([ + f'{template[i:i+2]}' + for i in range(len(template) - 1) + ]) + + char_count = { + **{ + c: 1 if c in template else 0 + for c + in mapping.values() + }, + **Counter(template), + } + + for _ in range(self.steps): + new_counter = { + pair: 0 + for pair + in mapping.keys() + } + + for key, value in counter.items(): + middle = mapping[key] + + char_count[middle] += value + new_counter[f'{key[0]}{middle}'] += value + new_counter[f'{middle}{key[1]}'] += value + + counter = new_counter + + return max(char_count.values()) - min(char_count.values()) + +class AssignmentOne(Assignment): + example_result = 1588 + steps = 10 + +class AssignmentTwo(Assignment): + max_pre_calculations = 4 + example_result = 2188189693529 + 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 @@ +NNCB + +CH -> B +HH -> N +CB -> H +NH -> C +HB -> C +HC -> B +HN -> C +NN -> C +BH -> H +NC -> B +NB -> B +BN -> B +BB -> N +BC -> B +CC -> N +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 @@ +COPBCNPOBKCCFFBSVHKO + +NS -> H +FS -> O +PO -> C +NV -> N +CK -> B +FK -> N +PS -> C +OF -> F +KK -> F +PP -> S +VS -> K +VB -> V +BP -> P +BB -> K +BF -> C +NN -> V +NO -> F +SV -> C +OK -> N +PH -> P +KV -> B +PN -> O +FN -> V +SK -> V +VC -> K +BH -> P +BO -> S +HS -> H +HK -> S +HC -> S +HF -> B +PC -> C +CF -> B +KN -> H +CS -> N +SP -> O +VH -> N +CC -> K +KP -> N +NP -> C +FO -> H +FV -> N +NC -> F +KB -> N +VP -> O +KO -> F +CP -> F +OH -> F +KC -> H +NB -> F +HO -> P +SC -> N +FF -> B +PB -> H +FB -> K +SN -> B +VO -> K +OO -> N +NF -> B +ON -> P +SF -> H +FP -> H +HV -> B +NH -> B +CO -> C +PV -> P +VV -> K +KS -> P +OS -> S +SB -> P +OC -> N +SO -> K +BS -> B +CH -> V +PK -> F +OB -> P +CN -> N +CB -> N +VF -> O +VN -> K +PF -> P +SH -> H +FH -> N +HP -> P +KF -> V +BK -> H +OP -> C +HH -> F +SS -> V +BN -> C +OV -> F +HB -> P +FC -> C +BV -> H +VK -> S +NK -> K +CV -> K +HN -> K +BC -> K +KH -> P -- cgit v1.2.3