From e1ab79cc94d48e4c6d2c25177122fedd3113973c Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Mon, 28 Nov 2022 23:51:45 +0100 Subject: [PATCH] 14 --- 2021/14/README.md | 13 ++++++ 2021/14/input.txt | 103 +++++++++++++++++++++++++++++++++++++++++ 2021/14/python/main.py | 86 ++++++++++++++++++++++++++++++++++ 2021/14/test.txt | 18 +++++++ 4 files changed, 220 insertions(+) create mode 100644 2021/14/README.md create mode 100644 2021/14/input.txt create mode 100644 2021/14/python/main.py create mode 100644 2021/14/test.txt diff --git a/2021/14/README.md b/2021/14/README.md new file mode 100644 index 0000000..e19a636 --- /dev/null +++ b/2021/14/README.md @@ -0,0 +1,13 @@ +# 14 + +This was nice! Similar to the Lanterfish one. The naive way is to have a big string, but that becomes unfeasible quickly. + +The solution was to keep track of the number of combinations available and at the end divide by 2 the counted elements. Rounding up so that the first and last element is not neglected as they are not doubled. + +
+ Solutions +
    +
  1. 2988
  2. +
  3. 3572761917024
  4. +
+
diff --git a/2021/14/input.txt b/2021/14/input.txt new file mode 100644 index 0000000..3ee0856 --- /dev/null +++ b/2021/14/input.txt @@ -0,0 +1,103 @@ +VFHKKOKKCPBONFHNPHPN + +VS -> B +HK -> B +FO -> P +NC -> F +VN -> C +BS -> O +HS -> K +NS -> C +CV -> P +NV -> C +PH -> H +PB -> B +PK -> K +HF -> P +FV -> C +NN -> H +VO -> K +VP -> P +BC -> B +KK -> S +OK -> C +PN -> H +SB -> V +KO -> P +KH -> C +KS -> S +FP -> B +PV -> B +BO -> C +OS -> H +NB -> S +SP -> C +HN -> N +FN -> B +PO -> O +FS -> O +NH -> B +SO -> P +OB -> S +KC -> C +OO -> H +BB -> V +SC -> F +NP -> P +SH -> C +BH -> O +BP -> F +CC -> S +BN -> H +SS -> P +BF -> B +VK -> P +OV -> H +FC -> S +VB -> S +PF -> N +HH -> O +HC -> V +CH -> B +HP -> H +FF -> H +VF -> V +CS -> F +KP -> F +OP -> H +KF -> F +PP -> V +OC -> C +PS -> F +ON -> H +BK -> B +HV -> S +CO -> K +FH -> C +FB -> F +OF -> V +SN -> S +PC -> K +NF -> F +NK -> P +NO -> P +CP -> P +CK -> S +HB -> H +BV -> C +SF -> K +HO -> H +OH -> B +KV -> S +KN -> F +SK -> K +VH -> S +CN -> S +VC -> P +CB -> H +SV -> S +VV -> P +CF -> F +FK -> F +KB -> V + diff --git a/2021/14/python/main.py b/2021/14/python/main.py new file mode 100644 index 0000000..cd198d2 --- /dev/null +++ b/2021/14/python/main.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python + +import math +from collections import defaultdict +from os.path import dirname, join + +# Day 14 + +# Common + + +def read_input(filename): + data = join(dirname(__file__), '..', filename) + with open(data) as f: + return f.read().strip() + + +test = read_input('test.txt') +data = read_input('input.txt') + + +class Polymer: + def __init__(self, state: str, insertions: dict[str, str]): + self.state = state + self.insertions = insertions + self.states: defaultdict[str, int] = defaultdict(lambda: 0) + + for i in range(len(self) - 1): + pair = self.state[i:i+2] + self.states[pair] += 1 + + def __len__(self): + return len(self.state) + + def step(self): + new = defaultdict(lambda: 0) + for state, count in self.states.items(): + if count == 0: + continue + insertion = self.insertions[state] + for key in [state[0] + insertion, insertion + state[1]]: + new[key] += count + self.states = new + + def score(self): + elements = defaultdict(lambda: 0) + for state, count in self.states.items(): + for element in state: + elements[element] += count + for element, count in elements.items(): + elements[element] = math.ceil(elements[element]/2) + return max(elements.values()) - min(elements.values()) + + def flag(self, steps: int): + for _ in range(steps): + self.step() + print(self.score()) + + @staticmethod + def parse(data: str): + state, raw = data.split('\n\n') + lines = [ + l.split(' -> ') + for l in raw.split('\n') + ] + insertions = { + i[0]: i[1] + for i in lines + } + return Polymer(state.strip(), insertions) + + +# 1 +print('1.') + +polymer = Polymer.parse(test) +polymer.flag(10) +polymer = Polymer.parse(data) +polymer.flag(10) + +# 2 +print('\n2.') +polymer = Polymer.parse(test) +polymer.flag(40) +polymer = Polymer.parse(data) +polymer.flag(40) diff --git a/2021/14/test.txt b/2021/14/test.txt new file mode 100644 index 0000000..b5594dd --- /dev/null +++ b/2021/14/test.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