-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday08_part1.py
More file actions
executable file
·66 lines (51 loc) · 1.61 KB
/
Copy pathday08_part1.py
File metadata and controls
executable file
·66 lines (51 loc) · 1.61 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
64
65
66
#!/usr/bin/env python
# Imports
import fileinput
import math
from itertools import combinations
from collections import defaultdict
from functools import reduce
import heapq
# Constants
TUPLE = ","
def find_shortest_connections(coords, limit):
return heapq.nsmallest(
limit,
(
(math.dist(coord_1, coord_2), coord_1, coord_2)
for coord_1, coord_2 in combinations(coords, 2)
),
)
def find_n_largest_connected_components(coords, edges, n):
# Create an adjacency list from edges
graph = defaultdict(list)
for _, u, v in edges:
graph[u].append(v)
graph[v].append(u)
# Function to perform DFS and find all connected nodes
def dfs(node, visited):
visited.add(node)
component_size = 1
for neighbor in graph[node]:
if neighbor not in visited:
component_size += dfs(neighbor, visited)
return component_size
visited = set()
component_sizes = []
for coord in coords:
if coord not in visited:
size = dfs(coord, visited)
component_sizes.append(size)
# Sort sizes and return the top 3
component_sizes.sort(reverse=True)
return component_sizes[:n]
def main():
junction_boxes = [
(int(x), int(y), int(z))
for x, y, z in (row.strip().split(TUPLE) for row in fileinput.input())
]
cables = find_shortest_connections(junction_boxes, 1000)
largest_circuits = find_n_largest_connected_components(junction_boxes, cables, 3)
print(reduce(lambda x, y: x * y, largest_circuits))
if __name__ == "__main__":
main()