-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday12.py
38 lines (29 loc) · 811 Bytes
/
day12.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
from collections import deque
with open('./inputs/12.txt') as f:
instructions = f.readlines()
graph = {}
for line in instructions:
pipe, neighbours = line.split(' <-> ')
pipe = int(pipe)
neighbours = [int(n) for n in neighbours.split(', ')]
graph[pipe] = neighbours
def bfs(starting_point):
connections = set()
que = deque([starting_point])
while que:
current = que.popleft()
connections.add(current)
for node in graph[current]:
if node not in connections:
que.append(node)
return connections
seen = bfs(0)
islands = 1
first_island_size = len(seen)
for pipe in graph:
if pipe not in seen:
pipe_island = bfs(pipe)
islands += 1
seen |= pipe_island
print(first_island_size)
print(islands)