1
0

day 18 part 2 python

This commit is contained in:
2024-12-20 23:47:24 +01:00
parent a765ed1965
commit 455d17f0a6

View File

@@ -7,10 +7,6 @@ with open('input/day18.txt', 'r') as f:
points = parse_input(data.strip().split('\n')) points = parse_input(data.strip().split('\n'))
points = set(points[:1024])
start = (0, 0)
goal = (70, 70)
w = 70
# points = set([ # points = set([
# (5, 4), # (5, 4),
@@ -42,24 +38,30 @@ w = 70
# goal = (6, 6) # goal = (6, 6)
# w = 6 # w = 6
def sp(points, w):
visited = dict() visited = dict()
stack = [(0, 0, 0)] stack = [(0, 0, 0)]
while len(stack) > 0: while len(stack) > 0:
# (x, y, c) = stack.pop(len(stack)-1)
(x, y, c) = stack.pop(0) (x, y, c) = stack.pop(0)
print("took " + str((x, y, c))) if (x, y) == (w, w):
if (x, y) == goal: return c
print(c)
break
if (x, y) in visited: if (x, y) in visited:
continue continue
visited[(x, y)] = c visited[(x, y)] = c
for (a, b) in [(x+nx, y+ny) for (nx, ny) in [(0, 1), (1, 0), (0, -1), (-1, 0)]]: for (a, b) in [(x+nx, y+ny) for (nx, ny) in [(0, 1), (1, 0), (0, -1), (-1, 0)]]:
print("checking " + str((a, b)))
if 0 <= a <= w and 0 <= b <= w and (a, b) not in points: if 0 <= a <= w and 0 <= b <= w and (a, b) not in points:
stack.append((a, b, c + 1)) stack.append((a, b, c + 1))
print("added something")
# 2224 too high return None
print("task 1: " + str(sp(set(points[:1024]), 70)))
for take in range(1024, len(points)):
solution = sp(set(points[:take]), 70)
if not solution:
print("task 2: " + str(points[take - 1]))
break