day 18 part 2 python
This commit is contained in:
50
day18.py
50
day18.py
@@ -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
|
||||||
|
|
||||||
visited = dict()
|
|
||||||
stack = [(0, 0, 0)]
|
def sp(points, w):
|
||||||
while len(stack) > 0:
|
visited = dict()
|
||||||
# (x, y, c) = stack.pop(len(stack)-1)
|
stack = [(0, 0, 0)]
|
||||||
(x, y, c) = stack.pop(0)
|
while len(stack) > 0:
|
||||||
print("took " + str((x, y, c)))
|
(x, y, c) = stack.pop(0)
|
||||||
if (x, y) == goal:
|
if (x, y) == (w, w):
|
||||||
print(c)
|
return c
|
||||||
|
|
||||||
|
if (x, y) in visited:
|
||||||
|
continue
|
||||||
|
visited[(x, y)] = c
|
||||||
|
|
||||||
|
for (a, b) in [(x+nx, y+ny) for (nx, ny) in [(0, 1), (1, 0), (0, -1), (-1, 0)]]:
|
||||||
|
if 0 <= a <= w and 0 <= b <= w and (a, b) not in points:
|
||||||
|
stack.append((a, b, c + 1))
|
||||||
|
|
||||||
|
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
|
break
|
||||||
|
|
||||||
if (x, y) in visited:
|
|
||||||
continue
|
|
||||||
visited[(x, y)] = c
|
|
||||||
|
|
||||||
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:
|
|
||||||
stack.append((a, b, c + 1))
|
|
||||||
print("added something")
|
|
||||||
|
|
||||||
# 2224 too high
|
|
||||||
|
|||||||
Reference in New Issue
Block a user