-
Notifications
You must be signed in to change notification settings - Fork 0
/
day25.py
35 lines (23 loc) · 986 Bytes
/
day25.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
# Advent of Code 2018, Day 25
# (c) blu3r4y
import numpy as np
import networkx as nx
from itertools import product
def part1(points):
graph = nx.Graph()
for i, j in product(range(len(points)), repeat=2):
# two points share a constellation if the are <= 3 apart
if manhattan(points[i], points[j]) <= 3:
graph.add_edge(i, j)
# number of constellations
return nx.number_connected_components(graph)
def manhattan(a, b):
return np.sum(np.abs(a - b))
def parse(lines):
return np.array([tuple(map(int, line.strip().split(','))) for line in lines])
if __name__ == "__main__":
print(part1(parse(open(r"../assets/day25_demo1.txt").readlines())))
print(part1(parse(open(r"../assets/day25_demo2.txt").readlines())))
print(part1(parse(open(r"../assets/day25_demo3.txt").readlines())))
print(part1(parse(open(r"../assets/day25_demo4.txt").readlines())))
print(part1(parse(open(r"../assets/day25.txt").readlines())))