-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay12.py
45 lines (35 loc) · 1.09 KB
/
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
39
40
41
42
43
44
45
# AOC17 day 12
def get_connected(node0, net):
con_list = []
to_check = [node0]
while len(to_check):
current = to_check.pop(0)
if current not in con_list:
con_list.append(current)
to_check.extend(net[current])
return con_list
# clears the net, oops ¯\_(ヅ)_/¯
def count_groups(net):
group_counter = 0
while len(net):
connected = get_connected(next(iter(net)), net)
group_counter += 1
for node in connected:
del net[node]
return group_counter
def make_net(data):
net = {}
for line in data.split("\n"):
words = line.split()
net[int(words[0])] = list(map(lambda x: int(x.replace(",", "")), words[2:]))
return net
def load_data(f_name):
with open(f_name, "r") as f:
data_read = f.read()
return data_read
def run():
data = load_data("Day12.txt")
net = make_net(data)
connected_to_0 = get_connected(0, net)
print(f"There are {len(connected_to_0)} programs connected to program 0")
print(f"There are {count_groups(net)} groups of programs")