aboutsummaryrefslogtreecommitdiff
path: root/2021/14/puzzles.py
blob: e162ffe79198029d7dc4042fc9b0249a15cc2324 (plain) (blame)
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
#!/usr/bin/env python3

from collections import Counter


def main() -> None:
	with open("input", "r", encoding="utf-8") as f:
		formula = f.readline().strip()
		f.readline()

		data: dict[str, str] = {}
		for line in f.readlines():
			x, y = line.strip().split(" -> ")
			data[(x[0], x[1])] = y

	pairs = Counter(zip(formula, formula[1:]))

	for _ in range(LOOPS):
		new: Counter[str] = Counter()
		for pair in pairs:
			new[(pair[0], data[pair])] += pairs[pair]
			new[(data[pair], pair[1])] += pairs[pair]
		pairs = new

	# Ran out of memory, lol
	# chars = Counter(flatten(map(lambda p: (p[0],) * pairs[p], pairs)))

	chars: Counter[str] = Counter()
	for pair in pairs:
		chars[pair[0]] += pairs[pair]
	chars[formula[-1]] += 1

	print(chars.most_common()[0][1] - chars.most_common()[-1][1])


if __name__ == "__main__":
	main()