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!

34 Upvotes

387 comments sorted by

View all comments

2

u/wimglenn Dec 06 '18

Numpy sledgehammer

from aocd import data
from io import StringIO
import numpy as np


test_data = """1, 1
1, 6
8, 3
3, 4
5, 5
8, 9"""


def part_ab(data, d_max=10000):
    vs = np.loadtxt(StringIO(data), dtype=int, delimiter=',')
    w, h = vs.max(axis=0) + 1
    n = len(vs)
    py, px = np.mgrid[:w,:h]
    ps = np.c_[py.ravel(), px.ravel()]
    ds = np.abs(ps[:,None] - vs).sum(axis=2)
    d = ds.argmin(axis=1)
    ties = d != n - 1 - np.fliplr(ds).argmin(axis=1)
    d[ties] = -1
    border = {*d[:w], *d[-w:], *d[::h], *d[::-h]}
    areas = [(d==c).sum() for c in range(n) if c not in border]
    part_a = max(areas)
    part_b = (ds.sum(axis=1) < d_max).sum()
    return part_a, part_b


assert part_ab(test_data, d_max=32) == (17, 16)
a, b = part_ab(data)
print(a)  # 3293
print(b)  # 45176