Can anyone take a look at my part 1 onegaishimasu? It looks correct to me.
from collections import deque
def Day19(data):
silver = 0; gold = 0
matrix = [list(x) for x in data.strip().splitlines()]
cols = len(matrix[0])
rows = len(matrix)
walls = set()
start = tuple()
end = tuple()
for y in range(rows):
for x in range(cols):
if matrix[y][x] == "#":
walls.add((y,x))
if matrix[y][x] == "S":
start = (y,x)
if matrix[y][x] == "E":
end = (y,x)
def bfs():
q = deque([(start, 0)]) # ((y, x), steps)
visited = set()
while q:
(y, x), steps = q.popleft()
if (y, x) == end:
return steps
if (y, x) in visited:
continue
visited.add((y, x))
for dy, dx in ((1,0), (0,1), (-1,0), (0,-1)):
ny, nx = y + dy, x + dx
if not (0 <= ny < rows and 0 <= nx < cols):
continue
if (ny, nx) in walls:
continue
q.append(((ny, nx), steps + 1))
return float("inf")
total_steps = bfs()
tried = set()
for w1 in walls:
for dy, dx in ((1,0), (0,1), (-1,0), (0,-1)):
w2 = (w1[0] + dy, w1[1] + dx)
if w2 not in walls:
continue
if (w1, w2) in tried:
continue
walls.remove(w1)
walls.remove(w2)
if total_steps - bfs() >= 100:
silver += 1
walls.add(w1)
walls.add(w2)
tried.add((w1, w2))
tried.add((w2, w1))
print(total_steps)
print(silver)
return (silver, 0)