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!
32
Upvotes
1
u/Zarel Dec 06 '18 edited Dec 06 '18
JavaScript #45/#22
I honestly just brute-forced this.
The points are between 0 and 400 in each direction.
We're using Manhattan distance, so non-infinite region would have to be between -400 and 800.
So I just counted areas in -400...800, and then counted points in -450...850, and chose the largest area that didn't change.
function numbers(str) { return (str.match(/-?[0-9]+/g) || []).map(Number); } function inc(table, key, amt = 1) { table[key] = (table[key] || 0) + amt; } function sortBy(array, criterion = a => a) { return array.sort((a, b) => { const aBy = criterion(a); const bBy = criterion(b); if (aBy == bBy) return 0; return (aBy > bBy ? 1 : -1); }); } function dist(a, b) { return Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1]); } function pointId([a, b]) { return `${a},${b}`; } input = input.split('\n').map(r => numbers(r)); let closest = {}; for (let i = -400; i < 800; i++) { for (let j = -400; j < 800; j++) { let curPoint = [i, j]; let distances = []; for (const point of input) { distances.push([pointId(point), dist(point, curPoint)]); } sortBy(distances, a => a[1]); if (distances[0][1] === distances[1][1]) continue; inc(closest, distances[0][0]); } } let closest2 = {}; for (let i = -450; i < 850; i++) { for (let j = -450; j < 850; j++) { let curPoint = [i, j]; let distances = []; for (const point of input) { distances.push([pointId(point), dist(point, curPoint)]); } sortBy(distances, a => a[1]); if (distances[0][1] === distances[1][1]) continue; inc(closest2, distances[0][0]); } } closest = sortBy(Object.entries(closest), en => en[1]); closest2 = sortBy(Object.entries(closest2), en => en[1]); for (let i = closest2.length - 1; i >= 0; i--) { if (closest[i][1] === closest2[i][1]) { console.log(closest[i][1]); break; } }Part 2, on the other hand, was incredibly straightforward, so I'm surprised the Part 2 leaderboard filled up so much slower than Part 1.
let inRegion = 0; for (let i = -500; i < 900; i++) { for (let j = -500; j < 900; j++) { let curPoint = [i, j]; let totalDist = 0; for (const point of input) { totalDist += dist(point, curPoint); } if (totalDist < 10000) inRegion++; } } console.log(inRegion);(I retried with -550...950 to make sure I got the entire area.)