This commit is contained in:
Sijmen Schoon 2021-12-14 14:04:51 +01:00
parent d199f9095b
commit b4877b5cac
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48
2 changed files with 50 additions and 0 deletions

2
.gitignore vendored
View file

@ -1,5 +1,7 @@
*.in *.in
.metals/ .metals/
profile.txt
# binaries # binaries
day?? day??
target/

48
day14.py Normal file
View file

@ -0,0 +1,48 @@
#!/usr/bin/env python3
import fileinput
from timeit import timeit
from collections import Counter, defaultdict
lines = [str(line) for line in fileinput.input()]
def main():
template = [ord(c) for c in lines[0].strip()]
rules = defaultdict(dict)
for l in lines[2:]:
rules[ord(l[0])][ord(l[1])] = ord(l[6])
polymer_counter = Counter(template)
counter = defaultdict(lambda: defaultdict(int))
for i in range(len(template) - 1):
counter[template[i]][template[i + 1]] += 1
def step():
nonlocal counter
new_counter = defaultdict(lambda: defaultdict(int))
for a, subcounter in counter.items():
for c, count in subcounter.items():
b = rules[a][c]
new_counter[a][b] += count
new_counter[b][c] += count
polymer_counter[b] += count
counter = new_counter
def run(iterations):
for _ in range(iterations):
step()
counts = polymer_counter.most_common()
return counts[0][1] - counts[-1][1]
part1 = run(10)
part2 = run(30) # 30 because it inherits the state from part 1
return part1, part2
if __name__ == "__main__":
print(main())
print(f"{timeit(main, number=2000) / 2:.3f} ms")