DEV Community

Discussion on: AoC Day 6: Chronal Coordinates

Collapse
 
r0f1 profile image
Florian Rohrer

Python using a kd-tree.

from collections import defaultdict
from collections import Counter
from itertools import product
import numpy as np
from scipy.spatial import KDTree

with open("input.txt") as f:
    coords = [(int(x.split(",")[0]), int(x.split(",")[1])) for x in f]

xs, ys = [x for x, y in coords], [y for x, y in coords]

# Part 1

t = KDTree(coords)
d = defaultdict(int)
edge = set()
for i, j in product(range(max(xs)), range(max(ys))):
    points, dists = t.query((i, j), k=2, p=1)
    if i == 0 or j == 0 or i == max(xs)-1 or j == max(ys)-1:
        edge.add(int(dists[0]))
    if points[0] != points[1]:
        d[(i,j)] = dists[0]

for p, area in Counter(d.values()).most_common():
    if p not in edge:
        print(area)
        break

# Part 2
def dist(x, y):
    return sum(abs(x-a)+abs(y-b) for a, b in coords)

print(sum(1 for i, j in product(range(max(xs)), range(max(ys))) if dist(i, j) < 10000))