From: Maximilian Friedersdorff Date: Thu, 16 Dec 2021 22:40:09 +0000 (+0000) Subject: Try day 14 X-Git-Url: https://git.friedersdorff.com/?a=commitdiff_plain;h=5accd75b77aeb92f15e2a9fac235c88a8f7c86fe;p=max%2Fadvent_of_code_2021.git Try day 14 --- diff --git a/14_1.py b/14_1.py new file mode 100644 index 0000000..2c6ce00 --- /dev/null +++ b/14_1.py @@ -0,0 +1,40 @@ +""" Pair insertion """ +import collections + +insertion_rules = {} + +with open("14_input.txt") as f: + polymer_template = f.readline().strip() + f.readline() + + for line in f: + pair, insert = line.strip().split(" -> ") + + insertion_rules[pair] = insert + +extended_template = [] + +expected_length = len(polymer_template) + +for gen in range(10): + for i in range(len(polymer_template) - 1): + + pair = polymer_template[i:i+2] + extended_template += [ + polymer_template[i], + insertion_rules[pair], + ] + + polymer_template = "".join( + extended_template + [polymer_template[i + 1]] + ) + extended_template = [] + expected_length += expected_length - 1 + + assert len(polymer_template) == expected_length, "Length unexpected" + +letter_count = collections.defaultdict(int) +for letter in polymer_template: + letter_count[letter] += 1 + +print(max(letter_count.values()) - min(letter_count.values())) diff --git a/14_2.py b/14_2.py new file mode 100644 index 0000000..0a5046b --- /dev/null +++ b/14_2.py @@ -0,0 +1,37 @@ +""" Pair insertion """ +import pprint +import collections + +insertion_rules = {} + +polymer_pairs = collections.defaultdict(int) + +letter_count = collections.defaultdict(int) + +with open("14_input.txt") as f: + polymer_template = f.readline().strip() + f.readline() + + for line in f: + pair, insert = line.strip().split(" -> ") + insertion_rules[pair] = insert + +for i in range(len(polymer_template) - 1): + pair = str(polymer_template[i:i+2]) + polymer_pairs[str(polymer_template[i:i+2])] += 1 + +print(polymer_pairs) + +for _ in range(40): + new_polymer_pairs = collections.defaultdict(int) + for pair, n in polymer_pairs.items(): + insert = insertion_rules[pair] + + new_polymer_pairs[f"{pair[0]}{insert}"] += n + new_polymer_pairs[f"{insert}{pair[0]}"] += n + letter_count[insert] += n + + polymer_pairs = new_polymer_pairs + +print(letter_count) +print(max(letter_count.values()) - min(letter_count.values()))