2015: d13: ex2: add solution
This commit is contained in:
parent
6e5baf38c2
commit
d19850f8d0
1 changed files with 47 additions and 0 deletions
47
2015/d13/ex2/ex2.py
Executable file
47
2015/d13/ex2/ex2.py
Executable file
|
|
@ -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()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue