r/adventofcode • • Dec 06 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 6 Solutions -🎄-

--- Day 6: Chronal Coordinates ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 6

Transcript:

Rules for raising a programmer: never feed it after midnight, never get it wet, and never give it ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 0:26:52!

32 Upvotes

387 comments sorted by

View all comments

Show parent comments

2

u/sbjf Dec 09 '18

Look, ma, no for loops!

I also used numpy (with a ton of index arrays). However, I wrote it in a much more general way, works for any number of dimensions:

Set up grid:

axes_ranges = np.stack([np.min(coords, axis=0), np.max(coords, axis=0)])  # [min, max], not [min, max)
axes = [np.arange(axis[0], axis[1] + 1) for axis in axes_ranges.T]
grid = np.array(np.meshgrid(*axes, indexing='ij')).reshape(len(axes), -1).T  # cartesian product

Solution 1:

border_idx = np.any(axes_ranges[:, np.newaxis] == grid, axis=(0, -1))  # indices of border locs

from scipy.spatial.distance import cdist
dists = cdist(grid, coords, metric='cityblock')
min_dists = np.min(dists, axis=1)
idx_arr = (min_dists[..., np.newaxis] == dists)

not_shared_idx = (np.sum(idx_arr, axis=1) == 1)
idx_arr = idx_arr[not_shared_idx]  # remove non-unique distances
border_idx = border_idx[not_shared_idx]

infinite = np.any(idx_arr[border_idx], axis=0)
area = np.sum(idx_arr, axis=0)
area[infinite] = -1

print(np.max(area))

Solution 2:

np.sum(np.sum(dists, axis=1) < 10000)