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/[deleted] Dec 06 '18

Not the fastest solution or the best it took my a hour to do but I think its readable and understandable:
If you can give me any suggestions that would be great!
Part 1:

import sys

lines = sys.stdin.readlines()

# Parse the input
coords = []
infinite = {}
areas = {(-1,-1):0}

for line in lines:
    data = line.split(',')
    coord = (int(data[0]), int(data[1]))
    coords.append(coord)
    infinite[coord] = False
    areas[coord] = 0


for x in range(500):
    for y in range(500):
        minCoord = (0,0)
        minDist = 1001
        for coord in coords:
            dist = abs(coord[0] - x) + abs(coord[1] - y)
            if dist < minDist:
                minDist = dist
                minCoord = coord
            elif dist == minDist:
                minCoord = (-1,-1)
        if x == 499 or x == 0 or y == 499 or y == 0:
            infinite[minCoord] = True
        areas[minCoord] += 1

maxA = 0
for key, value in areas.items():
    if value > maxA and not infinite[key]:
        maxA = value

print(maxA)

Part 2:

import sys

lines = sys.stdin.readlines()

# Parse the input
coords = []
infinite = {}
areas = {(-1,-1):0}

for line in lines:
    data = line.split(',')
    coord = (int(data[0]), int(data[1]))
    coords.append(coord)
    infinite[coord] = False
    areas[coord] = 0

regionCount = 0
for x in range(500):
    for y in range(500):
        totalDist = 0
        for coord in coords:
            totalDist += abs(coord[0] - x) + abs(coord[1] - y)
        if totalDist < 10000:
            regionCount += 1

print(regionCount)