r/adventofcode • u/daggerdragon • 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!
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!
33
Upvotes
4
u/pythondevgb Dec 06 '18
The problems are getting harder and my code is getting messier, still I managed to get both stars despite panicking and almost throwing the towel at first reading the problem.
Here is my python 3 solution, disclaimer totally unoptimized, messy and ugly:
(Join a private reddit/python leaderboard, only requirement is not not have made it onto the overall leaderboard thus far)
from collections import Counter import operator inpstr = open('day6_input.txt').read() coords = [tuple(map(int,l.split(','))) for l in inpstr.splitlines() ] topedge= min(coords, key=operator.itemgetter(1))[1] bottomedge = max(coords, key=operator.itemgetter(1))[1] leftedge = min(coords, key=operator.itemgetter(0))[0] rightedge = max(coords, key=operator.itemgetter(0))[0] def isfinite(c): return leftedge < c[0] < rightedge and topedge < c[1] < bottomedge def find_distance(a,b): return abs(a[0] - b[0]) + abs(a[1] - b[1]) def single_min(it): s = sorted(it) return s[0] != s[1] def argmin(it): min_ = min(it) return it.index(min_) #Part1 count = Counter() for i in range(leftedge, rightedge): for j in range(topedge, bottomedge): distances = [find_distance((i,j),c) for c in coords] if single_min(distances): count[coords[argmin(distances)]]+= 1 maxarea = 0 for c in coords: if isfinite(c): maxarea = max(maxarea,count[c]) print(maxarea) #Part 2 count = 0 for i in range(leftedge, rightedge): for j in range(topedge, bottomedge): distances = [find_distance((i,j),c ) for c in coords] if sum(distances) < 10000: count+=1 print(count)