Add day 6
This commit is contained in:
parent
08eaeb02e6
commit
7af9677207
4 changed files with 75 additions and 0 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -7,3 +7,5 @@
|
||||||
*.hi
|
*.hi
|
||||||
*.o
|
*.o
|
||||||
*.prof
|
*.prof
|
||||||
|
|
||||||
|
__pycache__
|
||||||
|
|
69
Day6/Day6.py
Normal file
69
Day6/Day6.py
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
from pprint import pprint
|
||||||
|
import fileinput
|
||||||
|
import itertools
|
||||||
|
import numpy
|
||||||
|
|
||||||
|
def distance(a, b):
|
||||||
|
ax, ay = a
|
||||||
|
bx, by = b
|
||||||
|
return abs(ax - bx) + abs(ay - by)
|
||||||
|
|
||||||
|
coordinates = []
|
||||||
|
for line in fileinput.input():
|
||||||
|
x, y = line.strip().split(", ")
|
||||||
|
coordinates.append((int(x), int(y)))
|
||||||
|
xs, ys = numpy.transpose(coordinates)
|
||||||
|
|
||||||
|
def nearest_neighbor(x, y):
|
||||||
|
closest = -1
|
||||||
|
dist = 10000
|
||||||
|
for i, coord in enumerate(coordinates):
|
||||||
|
d = distance((x, y), coord)
|
||||||
|
if d < dist:
|
||||||
|
closest = i
|
||||||
|
dist = d
|
||||||
|
elif d == dist:
|
||||||
|
closest = -1
|
||||||
|
|
||||||
|
return closest
|
||||||
|
|
||||||
|
def calc_farthest(padding):
|
||||||
|
|
||||||
|
xmin, xmax = min(xs) - padding, max(xs) + padding
|
||||||
|
ymin, ymax = min(ys) - padding, max(ys) + padding
|
||||||
|
w, h = xmax - xmin, ymax - ymin
|
||||||
|
|
||||||
|
grid = numpy.zeros((h + 1, w + 1), dtype=numpy.int16) - 1
|
||||||
|
for y, row in enumerate(grid):
|
||||||
|
for x in range(len(row)):
|
||||||
|
nn = nearest_neighbor(x + xmin, y + ymin)
|
||||||
|
if nn is not None:
|
||||||
|
grid[y, x] = nn
|
||||||
|
|
||||||
|
flat = grid.flatten()
|
||||||
|
flat = flat[flat >= 0]
|
||||||
|
|
||||||
|
return numpy.bincount(flat)
|
||||||
|
|
||||||
|
def part1():
|
||||||
|
small = calc_farthest(0)
|
||||||
|
big = calc_farthest(10)
|
||||||
|
print(small[small == big].max())
|
||||||
|
|
||||||
|
def part2():
|
||||||
|
padding = 5
|
||||||
|
max_dist = 10000
|
||||||
|
|
||||||
|
xmin, xmax = min(xs) - padding, max(xs) + padding
|
||||||
|
ymin, ymax = min(ys) - padding, max(ys) + padding
|
||||||
|
w, h = xmax - xmin, ymax - ymin
|
||||||
|
|
||||||
|
grid = numpy.zeros((h + 1, w + 1), dtype=numpy.int16) - 1
|
||||||
|
size = 0
|
||||||
|
for y, row in enumerate(grid):
|
||||||
|
for x in range(len(row)):
|
||||||
|
s = sum(distance((x + xmin, y + ymin), c) for c in coordinates)
|
||||||
|
if s < max_dist:
|
||||||
|
size += 1
|
||||||
|
|
||||||
|
print(size)
|
2
Day6/Day6A.py
Normal file
2
Day6/Day6A.py
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
from Day6 import part1
|
||||||
|
part1()
|
2
Day6/Day6B.py
Normal file
2
Day6/Day6B.py
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
from Day6 import part2
|
||||||
|
part2()
|
Loading…
Reference in a new issue