-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_14.py
56 lines (39 loc) · 2.15 KB
/
day_14.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from collections import Counter
from ..utils.file_utils import get_input
from ..utils.measurements import timed
@timed("Part01_dumb_version:")
def part01_slow(polymer_template: str, formulas: dict[str, str], steps: int = 10):
element_set: set[str] = set(formulas.values())
for step in range(steps):
new_polymer = polymer_template[0]
for cur_educt, next_educt in zip(polymer_template, polymer_template[1:]):
product = formulas.get(cur_educt+next_educt)
new_polymer += f"{'' if product is None else product}{next_educt}"
polymer_template = new_polymer
element_counts: set[str, int]= {k:polymer_template.count(k) for k in element_set}
return max(element_counts.values()) - min(element_counts.values())
def polymerization_score(polymer_template: str, formulas: dict[str, str], steps: int):
current_pairs = Counter(map(lambda t: t[0]+t[1], zip(polymer_template, polymer_template[1:])))
product_counts = Counter(polymer_template)
for step in range(steps):
for (educt_a, educt_b), occurrences in current_pairs.copy().items():
educts = educt_a+educt_b
product = formulas[educts]
current_pairs[educts] -= occurrences
current_pairs[educt_a+product] += occurrences
current_pairs[product + educt_b] += occurrences
product_counts[product] += occurrences
return max(product_counts.values()) - min(product_counts.values())
@timed("Part01:")
def part01_fast(polymer_template: str, formulas: dict[str, str]):
return polymerization_score(polymer_template, formulas, 10)
@timed("Part02:")
def part02_fast(polymer_template: str, formulas: dict[str, str]):
return polymerization_score(polymer_template, formulas, 40)
if __name__ == '__main__':
template_input, formula_inputs = get_input().split("\n\n")
polymer_template: str = template_input.strip()
formulas: dict[str, str] = {educt:product for educt, product in [line.strip().split(" -> ") for line in formula_inputs.split("\n")]}
part01_slow(polymer_template, formulas, 10)
part01_fast(polymer_template, formulas)
part02_fast(polymer_template, formulas)