-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18.py
48 lines (41 loc) · 1.16 KB
/
18.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# pylint: skip-file
# mypy: ignore-errors
# flake8: noqa
from collections import defaultdict, deque
input_value = open("18.txt", "r").read()
lines = input_value.split("\n")
rows = 71
columns = 71
# Cute outer loop to combine parts 1 and 2...
for i in range(1024, len(lines)):
grid = defaultdict(lambda: "#")
for r in range(rows):
for c in range(columns):
grid[r, c] = "."
for line in lines[:i]:
(c, r) = tuple(int(value) for value in line.split(","))
grid[r, c] = "#"
start = (0, 0)
target = (70, 70)
seen = set()
queue = deque()
queue.append((start, 0))
steps_found = None
while queue:
((r, c), steps) = queue.popleft()
if (r, c) in seen:
continue
seen.add((r, c))
if (r, c) == target:
steps_found = steps
break
for nr, nc in {(r - 1, c), (r, c - 1), (r + 1, c), (r, c + 1)}:
if grid[nr, nc] != "#":
queue.append(((nr, nc), steps + 1))
# Part 1:
if steps_found is not None and i == 1024:
print(steps)
# Part 2:
elif steps_found is None:
print(lines[i - 1])
break