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

1

u/AIGOOOMONA Dec 14 '18
Python 3 and using Counter. Took me a day and a half :(
My infinite_coord doesn't have all the coords neither, but it still gave me the answers


from collections import Counter

coord = [list(map(int, x.split(', '))) for x in open("input_d6.txt").readlines()]

x_min = min(coord, key=lambda k: k[0])[0]
x_max = max(coord, key=lambda k: k[0])[0]
y_min = min(coord, key=lambda k: k[1])[1]
y_max = max(coord, key=lambda k: k[1])[1]


def man_distance(x, y):
    return abs(x[0] - y[0]) + abs(x[1] - y[1])

def is_finite(x, y):
    return x_min < x < x_max and y_min < y < y_max

def equal_distance(dist):
    return sorted(dist)[0] == sorted(dist)[1]

area = Counter()
infinite_coord = set()
region = 0

for x in range(x_min, x_max):
    for y in range(y_min, y_max):
        dist = [man_distance([x, y], i) for i in coord]

        closest = dist.index(min(dist))

        if not is_finite(x, y) and closest not in infinite_coord:
            infinite_coord.add(closest)

        if not equal_distance(dist) and closest not in infinite_coord:
            area[closest] += 1

        # Part 2 - everything above is Part 1
        if sum(dist) < 10000:
            region += 1

print("Largest area: " + str(max(area.values())))

print("Region size: " + str(region))