Make day 10 2024 nice

This commit is contained in:
felixm 2024-12-10 20:01:11 -05:00
parent 83fbf59bd7
commit 96f15d07fc

View File

@ -5,32 +5,22 @@ from lib import get_data
data = get_data(__file__) data = get_data(__file__)
g = Grid2D(data) g = Grid2D(data)
starts = g.find("0")
p1, p2 = 0, 0
for start in starts:
n = 0
vertex = ((start,), start)
visited = set()
queue = deque([vertex])
nines = set()
p1 = 0
p2 = 0
for start in g.find("0"):
queue = deque([start])
all = set()
while queue: while queue:
path, pos = queue.popleft() node = queue.popleft()
if (path, pos) in visited: if g[node] == "9":
continue all.add(node)
visited.add((path, pos)) p2 += 1
if g[pos] == "9": for nb in g.neighbors_ort(node):
n += 1 if int(g[node]) + 1 == int(g[nb]):
nines.add(pos) queue.append(nb)
continue p1 += len(all)
for nb in g.neighbors_ort(pos):
if int(g[pos]) + 1 == int(g[nb]):
npath = tuple(list(path) + [nb])
queue.append((npath, nb))
p1 += len(nines)
p2 += n
print(p1) print(p1)
print(p2) print(p2)