-
Notifications
You must be signed in to change notification settings - Fork 3
/
day14.py
37 lines (26 loc) · 703 Bytes
/
day14.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
from day10 import knot_hashing
instructions = 'hwlqcszp'
maze = set()
for row in range(128):
word = f'{instructions}-{row}'
hex_hash = knot_hashing(word)
bin_hash = f'{int(hex_hash, 16):0128b}'
for i, n in enumerate(bin_hash):
if n == '1':
maze.add((row, i))
print(len(maze))
def dfs(start):
stack = [start]
while stack:
(x, y) = stack.pop()
for dx, dy in DELTAS:
candidate = x+dx, y+dy
if candidate in maze:
stack.append(candidate)
maze.remove(candidate)
DELTAS = ((1, 0), (-1, 0), (0, 1), (0, -1))
regions = 0
while maze:
dfs(maze.pop())
regions += 1
print(regions)