diff options
Diffstat (limited to '2021/14/puzzles.py')
-rw-r--r-- | 2021/14/puzzles.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/2021/14/puzzles.py b/2021/14/puzzles.py new file mode 100644 index 0000000..e162ffe --- /dev/null +++ b/2021/14/puzzles.py @@ -0,0 +1,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() |