Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def day18(s, *, size=71, n_step=1024, part2=False): # Brute-force.
- blocks = np.array([row.split(',') for row in s.splitlines()], int)[:, ::-1] + 1
- grid = np.pad(np.full((size, size), '.'), 1, constant_values='#')
- grid[tuple(blocks[:n_step].T)] = '#'
- def get_distance() -> int | None:
- yx, yxl = (1, 1), (size, size)
- visited = {yx}
- lst = [yx]
- for distance in itertools.count():
- lst2 = []
- for yx in lst:
- if yx == yxl:
- return distance
- y, x = yx
- for dy, dx in ((0, -1), (0, 1), (-1, 0), (1, 0)):
- yx2 = y + dy, x + dx
- if grid[yx2] == '.' and yx2 not in visited:
- visited.add(yx2)
- lst2.append(yx2)
- lst = lst2
- if not lst:
- return None
- if not part2:
- return get_distance()
- for y, x in blocks[n_step:]:
- grid[y, x] = '#'
- if get_distance() is None:
- return f'{x - 1},{y - 1}'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement