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!

31 Upvotes

387 comments sorted by

View all comments

1

u/lib20 Dec 14 '18

Red - part 1

Red []

time-start: now/time/precise

input-txt: read/lines %input.txt

coords: #()
forall input-txt [put coords load replace input-txt/1 ", " 'x 0]

min-width: 9999999999
max-width: 0
min-height: 9999999999
max-height: 0
foreach coord keys-of coords [
	if coord/1 < min-width [min-width: coord/1]
	if coord/1 > max-width [max-width: coord/1]
	if coord/2 < min-height [min-height: coord/2]
	if coord/2 > max-height [max-height: coord/2]
]

grid: #()
mi-w: min-width
mi-h: min-height
while [mi-w <= max-width] [
    while [mi-h <= max-height] [
		put grid as-pair mi-w mi-h 0
		mi-h: mi-h + 1
	]
	mi-w: mi-w + 1
	mi-h: min-height
]
 
foreach [k v] body-of grid [
	coord: find keys-of coords k
	either coord <> none [
		; -- grid point is a given coordinate
		put grid k to-block coord/1
	][
		; -- grid point value will have nearest coordinates
		; -- manhattan distance
		nearest: copy []
		min-distance: 99999999
		foreach c keys-of coords [
		    distance: k - c
			distance: (absolute distance/1) + (absolute distance/2)
			; -- test equality first
			if distance = min-distance [append nearest c]
			if distance < min-distance [
				min-distance: distance
				nearest: to-block c
			]
		]
		put grid k copy nearest
	]
]
 

foreach [k v] body-of grid [
	either (k/1 = min-width) or (k/1 = max-width)
		or
		(k/2 = min-height) or (k/2 = max-height) [
		; -- at borders, infinite, not accountable
		; -- tied grid points don't count
		unless (length? v) > 1 [put coords v/1 none]
	][
		; -- finite ones, the ones interesting
		if (find coords v/1) <> none [put coords v/1 (select coords v/1) + 1]
	]
]

coord: 0x0
size-largest: 0
foreach [k v] body-of coords [
    if v > size-largest [
		size-largest: v
		coord: k
	]
]

print rejoin ["Execution time: " now/time/precise - time-start]
print rejoin ["Coords point: " coord "    has largest size: " size-largest]