Repository navigation
Expand file tree
/
Copy pathday_23.py
More file actions
64 lines (55 loc) · 1.77 KB
/
Copy pathday_23.py
File metadata and controls
64 lines (55 loc) · 1.77 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import re
from datetime import datetime
time_start = datetime.now()
with open('day_23.input', 'r') as file:
lines = file.readlines()
number_pattern = r"^([a-z]+)(-)([a-z]+)"
connections_dict = {}
nodes = []
for line in lines:
part2 = re.match(number_pattern, line)
pc_1 = part2.group(1)
pc_2 = part2.group(3)
nodes.append([pc_1,pc_2])
if pc_1 in connections_dict:
connections_dict[pc_1].append(pc_2)
else:
connections_dict[pc_1] = [pc_2]
if pc_2 in connections_dict:
connections_dict[pc_2].append(pc_1)
else:
connections_dict[pc_2] = [pc_1]
# Find sets
sets = set()
for key, value in connections_dict.items():
for i in range(len(value)):
second = value[i]
for third in value[i+1:]:
if third in connections_dict[second]:
options = [key, second, third]
first = min(key, second, third)
options.remove(first)
last = max(key,second,third)
options.remove(last)
sets.add(str(first) + ":" + str(options[0]) + ":" + str(last))
total = 0
for seq in sets:
if seq[0] == "t" or seq[3] == "t" or seq[6] == "t":
total += 1
print("Part1", total)
print("Part1 time: ", datetime.now() - time_start)
part2_time = datetime.now()
pc_list = connections_dict.keys()
total_sets = []
for pc in pc_list:
new_set = {pc}
for pc_2 in connections_dict[pc]:
if all(pc_3 in connections_dict[pc_2] for pc_3 in new_set):
new_set.add(pc_2)
total_sets.append(new_set)
result = list(max(total_sets, key=len))
result.sort()
print("part2", ",".join(map(str,result)))
print("Part2 time: ", datetime.now() - part2_time)
print("Total time: ", datetime.now() - time_start)
#Total time: 0:00:30.041562