From 739cb2a3187c8c4280c596cfc49951f07c534e71 Mon Sep 17 00:00:00 2001 From: Pascal Lais Date: Tue, 14 Dec 2021 06:52:55 +0100 Subject: [PATCH] Add day 12 solution --- README.md | 1 + day-14/day-14.py | 73 +++++++++++++++++++++++++++++++++ day-14/input.txt | 102 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 day-14/day-14.py create mode 100644 day-14/input.txt diff --git a/README.md b/README.md index d944651..43612e2 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ Personal stats --------Part 1-------- --------Part 2-------- Day Time Rank Score Time Rank Score + 14 00:49:36 7350 0 00:50:14 2214 0 13 00:30:05 3269 0 00:37:02 2884 0 12 06:56:08 16817 0 16:08:35 26326 0 11 04:38:59 13748 0 04:42:36 13469 0 diff --git a/day-14/day-14.py b/day-14/day-14.py new file mode 100644 index 0000000..eb0cdda --- /dev/null +++ b/day-14/day-14.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +from pathlib import Path +from collections import Counter, defaultdict + + +def part_1(input): + result = 0 + template = input.pop(0).strip() + input.pop(0) + rules = {k.strip(): v.strip() + for line in input for k, v in [line.strip().split('->')]} + counts = defaultdict(int) + for i in range(len(template) - 1): + counts[template[i: i + 2]] += 1 + for _ in range(10): + tmp_cnts = counts.copy() + for k in tmp_cnts: + if k in rules: + c = tmp_cnts[k] + counts[k] -= c + counts[k[0] + rules[k]] += c + counts[rules[k] + k[1]] += c + char_counts = defaultdict(int) + for k in counts: + char_counts[k[0]] += counts[k] + char_counts[k[1]] += counts[k] + char_counts[template[0]] += 1 + char_counts[template[-1]] += 1 + for k in char_counts: + char_counts[k] //= 2 + char_counts = sorted(char_counts.items(), + key=lambda kv: kv[1]) + result = char_counts[-1][1] - char_counts[0][1] + print("Part 1 result:", result) + + +def part_2(input): + result = 0 + template = input.pop(0).strip() + input.pop(0) + rules = {k.strip(): v.strip() + for line in input for k, v in [line.strip().split('->')]} + counts = defaultdict(int) + for i in range(len(template) - 1): + counts[template[i: i + 2]] += 1 + for _ in range(40): + tmp_cnts = counts.copy() + for k in tmp_cnts: + if k in rules: + c = tmp_cnts[k] + counts[k] -= c + counts[k[0] + rules[k]] += c + counts[rules[k] + k[1]] += c + char_counts = defaultdict(int) + for k in counts: + char_counts[k[0]] += counts[k] + char_counts[k[1]] += counts[k] + char_counts[template[0]] += 1 + char_counts[template[-1]] += 1 + for k in char_counts: + char_counts[k] //= 2 + char_counts = sorted(char_counts.items(), + key=lambda kv: kv[1]) + result = char_counts[-1][1] - char_counts[0][1] + print("Part 2 result:", result) + + +input = list() +p = Path(__file__).with_name('input.txt') +with open(p) as f: + input = f.readlines() + part_1(input.copy()) + part_2(input.copy()) diff --git a/day-14/input.txt b/day-14/input.txt new file mode 100644 index 0000000..f7ea832 --- /dev/null +++ b/day-14/input.txt @@ -0,0 +1,102 @@ +NCOPHKVONVPNSKSHBNPF + +ON -> C +CK -> H +HC -> B +NP -> S +NH -> H +CB -> C +BB -> H +BC -> H +NN -> C +OH -> B +SF -> V +PB -> H +CP -> P +BN -> O +NB -> B +KB -> P +PV -> F +SH -> V +KP -> S +OF -> K +BS -> V +PF -> O +BK -> S +FB -> B +SV -> B +BH -> V +VK -> N +CS -> V +FV -> F +HS -> C +KK -> O +SP -> N +FK -> B +CF -> C +HP -> F +BF -> O +KC -> C +VP -> O +BP -> P +FF -> V +NO -> C +HK -> C +HV -> B +PK -> P +OV -> F +VN -> H +PC -> K +SB -> H +VO -> V +BV -> K +NC -> H +OB -> S +SN -> B +HF -> P +VF -> B +HN -> H +KS -> S +SC -> S +CV -> B +NS -> P +KO -> V +FS -> O +PH -> K +BO -> C +FH -> B +CO -> O +FO -> F +VV -> N +CH -> V +NK -> N +PO -> K +OK -> K +PP -> O +OC -> P +FC -> N +VH -> S +PN -> C +VB -> C +VS -> P +HO -> F +OP -> S +HB -> N +CC -> K +KN -> S +SK -> C +OS -> N +KH -> B +FP -> S +NF -> S +CN -> S +KF -> C +SS -> C +SO -> S +NV -> O +FN -> B +PS -> S +HH -> C +VC -> S +OO -> C +KV -> P \ No newline at end of file