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 08 '18

I am still trying to figure out why my code gives me the correct solution for part 2 only at the second position:

from collections import defaultdict, Counter

def manhattan(a, b):
	return abs(a[0]-b[0]) + abs(a[1]-b[1])

with open('testinput') as f:
	lines = f.read().splitlines()
	coords, xs, ys = [], [], []
	for line in lines:		
		coords.append(tuple((map(int, line.split(",")))))
		xs.append(coords[-1][0])
		ys.append(coords[-1][1])

	minx, maxx = min(xs), max(xs)
	miny, maxy = min(ys), max(ys)

	def find_closest(x,y):
		points_with_dist = defaultdict(list)

		for c in coords:
			d = manhattan((x,y), c)
			points_with_dist[d].append(c)
		return points_with_dist[min(dict(points_with_dist).keys())]

	field = defaultdict(list)

	for y in range(miny, maxy):
		for x in range(minx, maxx):
			cl = find_closest(x,y)
			if cl is not None:
				field[(x,y)] += cl

	uniques = [x[0] for x in field.values() if len(x) == 1]

	c = Counter(uniques)
	print(c.most_common())
	print(c.most_common()[0][1])

The biggest value of c.most_common() is wrong (for my input only. The example works), the second is correct. I cannot find my bug in the code... Does anybody have an idea?