aoc2022/d12/draw.py

59 lines
1.6 KiB
Python
Raw Normal View History

2022-12-14 13:34:10 +00:00
import sys
import networkx as netx
M = [ list(l) for l in sys.stdin.read().splitlines() ]
H = len(M)
W = len(M[0])
START = tuple()
END = tuple()
G = netx.DiGraph()
for x in range(H):
for y in range(W):
k = (x,y)
v = M[x][y]
if v == 'S':
START = k
v = 'a'
if v == 'E':
END = k
v = 'z'
v = ord(v)
for dx,dy in [(-1,0),(1,0),(0,-1),(0,1)]:
nx, ny = (x+dx, y+dy)
if nx>=0 and nx<H and ny>=0 and ny<W:
nk = (nx,ny)
nv = M[nx][ny]
if nv == 'S':
nv = 'a'
if nv == 'E':
nv = 'z'
nv = ord(nv)
dv = nv - v
if dv <=1: # can go down !
G.add_edge(k,nk)
# palette by https://gka.github.io/palettes/#/26|s|ffffe5,fe9929,662506|ffffe0,ff005e,93003a|1|1
C = [ '#ffffe5','#fff4d3','#ffeac1','#ffdfb0','#ffd49f','#fcca91','#f8c084','#f3b677','#edac6c','#e7a361','#e19a58','#da914e','#d38846','#cb803e','#c47737','#bc6f30','#b3672a','#ab5f24','#a3571f','#9a501a','#924816','#894112','#803a0e','#77330b','#6f2c09','#662506']
c2i = lambda x: int("0x"+x,16)
C = [ " ".join(map(str,map(c2i,[c[1:3],c[3:5],c[5:]]))) for c in C ]
for x in range(H):
for y in range(W):
v = M[x][y]
if v == 'S':
v = 'a'
if v == 'E':
v = 'z'
M[x][y] = C[ord(v)-ord("a")]
S = netx.shortest_path(G, START, END)
print("P3")
print("81 41")
print("255")
for (x,y) in S:
M[x][y] = "64 196 64"
print("\n".join("\n".join(l) for l in M))