diff options
author | Thomas Voss <thomasvoss@live.com> | 2021-12-06 08:48:22 +0100 |
---|---|---|
committer | Thomas Voss <thomasvoss@live.com> | 2021-12-06 08:48:22 +0100 |
commit | 0be87cad79b9db25be5a5e79e402148516d3a474 (patch) | |
tree | 003ded91e11084b7fbfd53f73690b7098d543ad0 /2015/13/puzzles.py | |
parent | 7a58c33804fde0a070aa6faabb0ca385c324f61a (diff) |
Add day 13 solutions
Diffstat (limited to '2015/13/puzzles.py')
-rw-r--r-- | 2015/13/puzzles.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/2015/13/puzzles.py b/2015/13/puzzles.py new file mode 100644 index 0000000..33e49e1 --- /dev/null +++ b/2015/13/puzzles.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + +import re +from collections import defaultdict +from itertools import permutations +from typing import DefaultDict + + +def main() -> None: + with open("input", "r", encoding="utf-8") as f: + entries = list( + map( + lambda e: [e[0], e[1], int(e[2]) if e[1] == "gain" else -int(e[2]), e[3]], + ( + re.split( + r"([^ ]+) would (gain|lose) (\d+) happiness units by sitting next to ([^" + r" ]+).\n", + line, + )[1:-1] + for line in f.readlines() + ), + ) + ) + + emap: DefaultDict[str, dict[str, int]] = defaultdict(lambda: {}) + for entry in entries: + emap[entry[0]][entry[3]] = entry[2] + # START PART 2 + emap[entry[0]]["Me"] = 0 + emap["Me"][entry[0]] = 0 + # END PART 2 + + print( + max( + sum( + emap[pair[0]][pair[1]] + emap[pair[1]][pair[0]] + for pair in tuple(zip(perm, perm[1:])) + ((perm[-1], perm[0]),) + ) + for perm in permutations(emap) + ) + ) + + +if __name__ == "__main__": + main() |