[Python/2016] Move solutions into .py files

This commit is contained in:
Felix Bargfeldt 2023-10-29 12:38:12 +01:00
parent 0269ad8fc3
commit 2514b1d11f
Signed by: Defelo
GPG key ID: 2A05272471204DD3
50 changed files with 1172 additions and 3386 deletions

80
Python/2016/24.py Normal file
View file

@ -0,0 +1,80 @@
from lib import *
input = read_input(2016, 24)
grid = input.splitlines()
nodes = {}
rnodes = {}
for i, line in enumerate(grid):
for j, c in enumerate(line):
if c.isnumeric():
nodes[int(c)] = j, i
rnodes[(j, i)] = int(c)
def asp(x, y):
queue = [(0, x, y)]
out = {}
visited = set()
while queue:
d, x, y = queue.pop(0)
if (x, y) in visited:
continue
visited.add((x, y))
if (x, y) in rnodes:
out[rnodes[(x, y)]] = d
for p, q in [(x, y - 1), (x, y + 1), (x - 1, y), (x + 1, y)]:
if p not in range(len(grid[0])) or q not in range(len(grid)) or grid[q][p] == "#":
continue
queue.append((d + 1, p, q))
return out
sp = {k: asp(*v) for k, v in nodes.items()}
best = 1e1337
for order in itertools.permutations(set(sp) - {0}):
pos = 0
cost = 0
for x in order:
cost += sp[pos][x]
pos = x
best = min(best, cost)
print(best)
best = 1e1337
for order in itertools.permutations(set(sp) - {0}):
pos = 0
cost = 0
for x in order:
cost += sp[pos][x]
pos = x
best = min(best, cost + sp[pos][0])
print(best)