diff --git a/2015/d13/ex2/ex2.py b/2015/d13/ex2/ex2.py new file mode 100755 index 0000000..a7fad12 --- /dev/null +++ b/2015/d13/ex2/ex2.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python + +import collections +import itertools +import sys + + +def solve(input: str) -> int: + def parse_line(input: str) -> tuple[str, str, int]: + split_input = input.removesuffix(".").split() + p, other = split_input[0], split_input[-1] + delta = int(split_input[3]) * (1 if split_input[2] == "gain" else -1) + return p, other, delta + + def parse(input: str) -> dict[str, dict[str, int]]: + res: dict[str, dict[str, int]] = collections.defaultdict(dict) + for p, neighbour, delta in map(parse_line, input.splitlines()): + res[p][neighbour] = delta + return res + + def score_seating(deltas: dict[str, dict[str, int]], seating: list[str]) -> int: + table = itertools.chain(seating, [seating[0]]) + return sum( + deltas[p1][p2] + deltas[p2][p1] for p1, p2 in itertools.pairwise(table) + ) + + def add_me(deltas: dict[str, dict[str, int]]) -> None: + deltas["me"] = {} + for other in deltas.keys(): + deltas["me"][other] = 0 + deltas[other]["me"] = 0 + + deltas = parse(input) + add_me(deltas) + return max( + score_seating(deltas, list(perm)) + for perm in itertools.permutations(deltas.keys()) + ) + + +def main() -> None: + input = sys.stdin.read() + print(solve(input)) + + +if __name__ == "__main__": + main()