diff --git a/14/14.py b/14/14.py new file mode 100755 index 0000000..9825834 --- /dev/null +++ b/14/14.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python +# 2021 - Advent Of Code - 14 + +from collections import Counter + + +def parse_file(file): + with open(file) as f: + poly_string = list(f.readline().strip()) + f.readline() + insertion_rules = dict([line.strip().split(' -> ') for line in f.readlines()]) + + return poly_string, insertion_rules + + +def step_poly(poly_str, ruleset): + new_p = [] + for i, p in enumerate(poly_str): + if i + 1 == len(poly_str): + break + new_p.append(p) + binome = '' + p + poly_str[i+1] + # print(f'binome {binome}') + found = ruleset.get(binome) + if found: + # print(f'found: {found}') + new_p.append(found) + + new_p.append(p) + return ''.join(new_p) + + +# poly, rules = parse_file('input_example.txt') +poly, rules = parse_file('input.txt') +print(f'poly: {poly}') +print(f'rules: {rules}') + +for s in range(10): + print(f'step: {s}') + poly = step_poly(poly, rules) + print(poly) + +print(Counter(poly)) +c = Counter(poly).most_common()[0][1] - Counter(poly).most_common()[-1][1] +print(c) diff --git a/14/input.txt b/14/input.txt new file mode 100644 index 0000000..ac9c0aa --- /dev/null +++ b/14/input.txt @@ -0,0 +1,102 @@ +BCHCKFFHSKPBSNVVKVSK + +OV -> V +CO -> V +CS -> O +NP -> H +HH -> P +KO -> F +VO -> B +SP -> O +CB -> N +SB -> F +CF -> S +KS -> P +OH -> H +NN -> O +SF -> K +FH -> F +VV -> B +VH -> O +BV -> V +KF -> K +CC -> F +NF -> H +VS -> O +SK -> K +HV -> O +CK -> K +VP -> F +HP -> S +CN -> K +OB -> H +NS -> F +PS -> S +KB -> S +VF -> S +FP -> H +BB -> N +HF -> V +CH -> N +BH -> F +KK -> B +OO -> N +NO -> K +BP -> K +KH -> P +KN -> P +OF -> B +VC -> F +NK -> F +ON -> O +OC -> P +VK -> O +SH -> C +NH -> C +FB -> B +FC -> K +OP -> O +PV -> V +BN -> V +PC -> K +PK -> S +FF -> C +SV -> S +HK -> H +NB -> C +OK -> C +PH -> B +SO -> O +PP -> F +KV -> V +FO -> B +FN -> H +HN -> C +VB -> K +CV -> O +BC -> C +CP -> S +FS -> S +KP -> V +BS -> V +BK -> B +PN -> C +PF -> S +HO -> V +NC -> N +SS -> N +BO -> P +BF -> N +NV -> P +PB -> K +HB -> H +VN -> H +FV -> B +FK -> K +PO -> S +SC -> S +HS -> S +KC -> F +HC -> S +OS -> K +SN -> N diff --git a/14/input_example.txt b/14/input_example.txt new file mode 100644 index 0000000..b5594dd --- /dev/null +++ b/14/input_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