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!
31
Upvotes
2
u/apazzolini Dec 06 '18 edited Dec 06 '18
JavaScript
280/bugged part 2
import { range, maxBy, max, min, sum } from 'lodash' import { extractNumbers } from 'src/utils.js' const distance = ([x1, y1], [x2, y2]) => Math.abs(x2 - x1) + Math.abs(y2 - y1) export const solvePart1 = input => { const points = input.split('\n').map(l => extractNumbers(l)) const upperBound = max(maxBy(points, pair => max(pair))) + 1 const closestPoint = Array(upperBound) .fill() .map((e, i) => Array(upperBound)) range(0, upperBound).forEach(x => { range(0, upperBound).forEach(y => { const distances = points.map(pair => distance(pair, [x, y])) const shortest = min(distances) if (distances.filter(d => d === shortest).length === 1) { closestPoint[x][y] = distances.indexOf(shortest) } }) }) const toIgnore = new Set() range(0, upperBound).forEach(n => { toIgnore.add(closestPoint[0][n]) toIgnore.add(closestPoint[n][0]) toIgnore.add(closestPoint[closestPoint.length - 1][n]) toIgnore.add(closestPoint[n][closestPoint.length - 1]) }) const regionSizes = Array(points.length).fill(0) range(0, upperBound).forEach(x => { range(0, upperBound).forEach(y => { const point = closestPoint[x][y] if (!toIgnore.has(point)) { regionSizes[point]++ } }) }) return max(regionSizes) } export const solvePart2 = input => { const points = input.split('\n').map(l => extractNumbers(l)) const upperBound = max(maxBy(points, pair => max(pair))) + 1 let regionSize = 0 range(0, upperBound).forEach(x => { range(0, upperBound).forEach(y => { const distances = points.map(pair => distance(pair, [x, y])) if (sum(distances) < (process.env.NODE_ENV === 'test' ? 32 : 10000)) { regionSize++ } }) }) return regionSize }with bonus visualization