diff options
author | Thomas Voss <thomasvoss@live.com> | 2021-12-14 06:42:36 +0100 |
---|---|---|
committer | Thomas Voss <thomasvoss@live.com> | 2021-12-14 06:42:36 +0100 |
commit | e9ae9ea8066c57f7065e94d40f510d45da6ad427 (patch) | |
tree | 4498c0117ecb27f9caa6be4d64f001ca227af591 /2021/14/puzzles.py | |
parent | 4f5b004db9a93d9ff356cdd3e6a9fa40421628a7 (diff) |
Add day 14 solutions
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() |