diff --git a/day14/input.txt b/day14/input.txt new file mode 100644 index 0000000..591c6c8 --- /dev/null +++ b/day14/input.txt @@ -0,0 +1,102 @@ +PBFNVFFPCPCPFPHKBONB + +KK -> S +FO -> B +PP -> O +HN -> S +CN -> H +VH -> P +BK -> B +VC -> N +CB -> H +OC -> K +BF -> P +FV -> K +SP -> F +OP -> K +SS -> B +NN -> O +CS -> K +CF -> K +FF -> S +SV -> P +OK -> S +CO -> F +OB -> K +BH -> B +HH -> S +VB -> V +KV -> H +CK -> V +NV -> N +SF -> V +PK -> H +PV -> N +FB -> O +BO -> K +FP -> N +OF -> N +FK -> O +VK -> V +NO -> V +NS -> C +KC -> S +VF -> V +BV -> N +CP -> K +PB -> V +CC -> S +NH -> B +CV -> P +SO -> V +NC -> O +HK -> K +SB -> H +OO -> V +HO -> P +PS -> B +BC -> P +KO -> C +KB -> C +VV -> F +BS -> F +HB -> B +KN -> S +FC -> C +SN -> S +HC -> O +HP -> F +BP -> V +ON -> K +BB -> K +KH -> O +NP -> H +KS -> N +SH -> K +VP -> O +PF -> O +HF -> S +BN -> S +NK -> C +FH -> O +CH -> B +KP -> B +FN -> K +OV -> P +VS -> K +OH -> V +PC -> F +VO -> H +SK -> S +PO -> O +KF -> N +NF -> V +NB -> C +PN -> O +FS -> C +PH -> F +VN -> S +OS -> V +HV -> H +HS -> B +SC -> C diff --git a/day14/part1.py b/day14/part1.py new file mode 100644 index 0000000..29f6b79 --- /dev/null +++ b/day14/part1.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +import re +from collections import Counter + +lines = (x.strip() for x in open("input.txt")) +first = next(lines) + +combinations = Counter(first[a:a+2] for a in range(len(first)-1)) +replacements = dict() + +next(lines) + +for line in lines: + before, insert = re.match(r"([A-Z]{2}) -> ([A-Z])", line).groups() + replacements[before] = (before[0]+insert, insert+before[1]) + +for _ in range(10): + nxt = {} + for before, afters in replacements.items(): + for after in afters: + add = combinations.get(before, 0) + nxt.get(after, 0) + if add: + nxt[after] = add + combinations = nxt + +letters = {} +for key, cnt in combinations.items(): + letters[key[0]] = letters.get(key[0], 0) + cnt + letters[key[1]] = letters.get(key[1], 0) + cnt + +print(letters) +print((max(*letters.values())+1) // 2 - (min(*letters.values())+1) // 2) diff --git a/day14/part2.py b/day14/part2.py new file mode 100644 index 0000000..cf76aad --- /dev/null +++ b/day14/part2.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +import re +from collections import Counter + +lines = (x.strip() for x in open("input.txt")) +first = next(lines) + +combinations = Counter(first[a:a+2] for a in range(len(first)-1)) +replacements = dict() + +next(lines) + +for line in lines: + before, insert = re.match(r"([A-Z]{2}) -> ([A-Z])", line).groups() + replacements[before] = (before[0]+insert, insert+before[1]) +print(combinations) + +for _ in range(40): + nxt = {} + for before, afters in replacements.items(): + for after in afters: + add = combinations.get(before, 0) + nxt.get(after, 0) + if add: + nxt[after] = add + combinations = nxt + print(combinations) + +letters = {} +for key, cnt in combinations.items(): + letters[key[0]] = letters.get(key[0], 0) + cnt + letters[key[1]] = letters.get(key[1], 0) + cnt + +print(letters) +print((max(*letters.values())+1) // 2 - (min(*letters.values())+1) // 2)