diff --git a/2024/d10.py b/2024/d10.py index 5203c74..54e056d 100644 --- a/2024/d10.py +++ b/2024/d10.py @@ -5,32 +5,22 @@ from lib import get_data data = get_data(__file__) 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: - path, pos = queue.popleft() - if (path, pos) in visited: - continue - visited.add((path, pos)) + node = queue.popleft() + if g[node] == "9": + all.add(node) + p2 += 1 - if g[pos] == "9": - n += 1 - nines.add(pos) - continue - - 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 + for nb in g.neighbors_ort(node): + if int(g[node]) + 1 == int(g[nb]): + queue.append(nb) + p1 += len(all) print(p1) print(p2)