From d6179dc2e6dde96b4f91ed638da35ceaaa0dc919 Mon Sep 17 00:00:00 2001 From: Peter Hudec Date: Tue, 14 Dec 2021 15:59:23 +0100 Subject: [PATCH] added day 14 --- 14/input01.txt | 102 ++++++++++++++++++++++++++++++++++++++++++ 14/input01_sample.txt | 18 ++++++++ 14/solve01.py | 36 +++++++++++++++ 14/solve02.py | 63 ++++++++++++++++++++++++++ 4 files changed, 219 insertions(+) create mode 100644 14/input01.txt create mode 100644 14/input01_sample.txt create mode 100644 14/solve01.py create mode 100644 14/solve02.py diff --git a/14/input01.txt b/14/input01.txt new file mode 100644 index 0000000..0cc87ed --- /dev/null +++ b/14/input01.txt @@ -0,0 +1,102 @@ +FNFPPNKPPHSOKFFHOFOC + +VS -> B +SV -> C +PP -> N +NS -> N +BC -> N +PB -> F +BK -> P +NV -> V +KF -> C +KS -> C +PV -> N +NF -> S +PK -> F +SC -> F +KN -> K +PN -> K +OH -> F +PS -> P +FN -> O +OP -> B +FO -> C +HS -> F +VO -> C +OS -> B +PF -> V +SB -> V +KO -> O +SK -> N +KB -> F +KH -> C +CC -> B +CS -> C +OF -> C +FS -> B +FP -> H +VN -> O +NB -> N +BS -> H +PC -> H +OO -> F +BF -> O +HC -> P +BH -> S +NP -> P +FB -> C +CB -> H +BO -> C +NN -> V +SF -> N +FC -> F +KK -> C +CN -> N +BV -> F +FK -> C +CF -> F +VV -> B +VF -> S +CK -> C +OV -> P +NC -> N +SS -> F +NK -> V +HN -> O +ON -> P +FH -> O +OB -> H +SH -> H +NH -> V +FF -> B +HP -> B +PO -> P +HB -> H +CH -> N +SN -> P +HK -> P +FV -> H +SO -> O +VH -> V +BP -> V +CV -> P +KP -> K +VB -> N +HV -> K +SP -> N +HO -> P +CP -> H +VC -> N +CO -> S +BN -> H +NO -> B +HF -> O +VP -> K +KV -> H +KC -> F +HH -> C +BB -> K +VK -> P +OK -> C +OC -> C +PH -> H \ No newline at end of file diff --git a/14/input01_sample.txt b/14/input01_sample.txt new file mode 100644 index 0000000..6c1c3a1 --- /dev/null +++ b/14/input01_sample.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 \ No newline at end of file diff --git a/14/solve01.py b/14/solve01.py new file mode 100644 index 0000000..843e5ee --- /dev/null +++ b/14/solve01.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +import re + +RE_INS = re.compile("(.*)\s+->\s+(.*)$") + +def iterate_polymer(polymer, rules): + result = polymer[0] + for i in range(0, len(polymer)-1): + k = polymer[i:i+2] + if k in rules.keys(): + result = result + rules[k]+k[1] + else: + result = result+k[1] + return result + +polymer = None +rules = dict() + +with open("input01.txt","r") as f: + polymer = f. readline().strip() + + for line in f: + line = line.strip() + if len(line) == 0: + continue + m = RE_INS.match(line) + if not m: + print(line) + rules[m.group(1)] = m.group(2) + +for i in range(10): + polymer = iterate_polymer(polymer, rules) + +count = [polymer.count(i) for i in set(polymer)] +print(max(count) - min(count)) \ No newline at end of file diff --git a/14/solve02.py b/14/solve02.py new file mode 100644 index 0000000..a9c4303 --- /dev/null +++ b/14/solve02.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python + +import re +from collections import defaultdict + +RE_INS = re.compile("(.*)\s+->\s+(.*)$") + +def tokenize_polymer(polymer): + result = defaultdict(int) + for i in range(0, len(polymer)-1): + k = polymer[i:i+2] + result[k] += 1 + return result + +def iterate_polymer(polymer, rules): + delta = defaultdict(int) + pairs = list(polymer.keys()) + for pair in pairs: + if pair not in rules: + continue + if polymer[pair] == 0: + continue + p1 = pair[0]+rules[pair] + p2 = rules[pair]+pair[1] + delta[p1] += polymer[pair] + delta[p2] += polymer[pair] + delta[pair] -= polymer[pair] + for pair in delta: + polymer[pair] += delta[pair] + return polymer + +polymer = None +rules = dict() +polymer_pairs = defaultdict(int) + +with open("input01.txt","r") as f: + polymer = f. readline().strip() + + for line in f: + line = line.strip() + if len(line) == 0: + continue + m = RE_INS.match(line) + if not m: + print(line) + rules[m.group(1)] = m.group(2) +for i in range(0, len(polymer)-1): + k = polymer[i:i+2] + polymer_pairs[k] += 1 + +polymer_pairs = tokenize_polymer(polymer) +for i in range(40): + polymer_pairs = iterate_polymer(polymer_pairs, rules) +# print(polymer_pairs) + +sums = defaultdict(int) +for i in polymer_pairs.keys(): + sums[i[1]] += polymer_pairs[i] +sums[polymer[0]] += 1 + +p_max = max([x for x in sums.values()]) +p_min = min([x for x in sums.values()]) +print(p_max - p_min)