summaryrefslogtreecommitdiff
path: root/14_2021.py
blob: d7a1d64b0fb02696d15c488bf0a52dbb15e96efb (plain)
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
import sys
from collections import Counter

infile = sys.argv[1] if len(sys.argv) > 1 else "day14data.txt"
S, rules = open(infile).read().split("\n\n")
R = {}

for line in rules.strip().split("\n"):
    start, end = line.strip().split(" -> ")
    R[start] = end

C1 = Counter()
for i in range(len(S) - 1):
    C1[S[i] + S[i + 1]] += 1

for t in range(41):
    if t in [10, 40]:
        CF = Counter()
        for k in C1:
            CF[k[0]] += C1[k]
        CF[S[-1]] += 1
        print(max(CF.values()) - min(CF.values()))
    C2 = Counter()
    for k in C1:
        C2[k[0] + R[k]] += C1[k]
        C2[R[k] + k[1]] += C1[k]
    C1 = C2