Part 1
data = open("Day 18 input.txt", "r").read().strip().split("\n")
size = 70
grid = set()
for i in range(1024):
grid.add(tuple(map(int, data[i].split(","))))
dirs = [[1,0],[0,1],[-1,0],[0,-1]]
queue = [[(0,0), 0]]
visited = set()
while True:
pos, cost = queue.pop(0)
if pos in visited: continue
visited.add(pos)
if pos == (size, size): break
for d in dirs:
if 0 <= pos[0] <= size and 0 <= pos[1] <= size and (pos[0]+d[0],pos[1]+d[1]) not in grid:
queue.append([(pos[0]+d[0],pos[1]+d[1]), cost+1])
print cost
Part 2
data = open("Day 18 input.txt", "r").read().strip().split("\n")
size = 70
grid = set()
dirs = [[1,0],[0,1],[-1,0],[0,-1]]
for num, i in enumerate(data):
print num
grid.add(tuple(map(int, i.split(","))))
queue = [[(0,0), 0]]
visited = set()
while len(queue):
pos, cost = queue.pop(0)
if pos in visited: continue
visited.add(pos)
if pos == (size, size): break
for d in dirs:
if 0 <= pos[0] <= size and 0 <= pos[1] <= size and (pos[0]+d[0],pos[1]+d[1]) not in grid:
queue.append([(pos[0]+d[0],pos[1]+d[1]), cost+1])
else:
print i
break
Easy BFS, not even using a proper deque brute force was still fast enough.
Can't remember the last time I used an else statement on a while loop, if ever.