-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay18-1.py
More file actions
38 lines (31 loc) · 951 Bytes
/
Day18-1.py
File metadata and controls
38 lines (31 loc) · 951 Bytes
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
from collections import deque
grid = []
x, y = [0, 0]
directions = {'R': (0, 1), 'L': (0, -1), 'U': (-1, 0), 'D': (1, 0)}
def dig(dir: tuple, steps: int):
global grid, x, y
for i in range(1, steps + 1):
y += dir[0]
x += dir[1]
grid.append((y, x))
def fill(grid: list, pos: tuple):
q = deque([pos])
while q:
current = q.popleft()
grid.append(current)
for d in directions.values():
newPos = tuple(sum(x) for x in list(zip(current, d)))
if newPos not in grid and newPos not in q:
q.append(newPos)
def findStartForFilling(topBorder: list, grid:list) -> tuple:
for r, c in topBorder:
if (r + 1, c) not in grid:
return r + 1, c
with open("input.txt") as f:
for line in f.readlines():
dir, steps, _ = line.split()
dig(directions[dir], int(steps))
topBorder = [x for x in grid if x[0] == min(grid, key=lambda x: x[0])[0]]
start = findStartForFilling(topBorder, grid)
fill(grid, start)
print("Result 1:", len(grid))