From 0be87cad79b9db25be5a5e79e402148516d3a474 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Mon, 6 Dec 2021 08:48:22 +0100 Subject: Add day 13 solutions --- 2015/13/puzzles.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 2015/13/puzzles.py (limited to '2015/13/puzzles.py') 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() -- cgit v1.2.3