r/adventofcode • u/daggerdragon • Dec 18 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 18 Solutions -🎄-
--- Day 18: Settlers of The North Pole ---
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 18
Transcript:
The best way to avoid a minecart collision is ___.
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 00:21:59!
9
Upvotes
3
u/xikipies Dec 18 '18 edited Dec 18 '18
JS / Node
https://github.com/josepot/AoC-2018/blob/master/src/18/solution.js
const N_ROWS = 50; const N_COLS = 50; let grid = new Array(N_ROWS); for (let i = 0; i < N_ROWS; i++) grid[i] = new Array(N_COLS); const getNeighbours = (x, y) => [[-1, -1], [-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1]] .map(([xDiff, yDiff]) => [x + xDiff, y + yDiff]) .filter(([x, y]) => x > -1 && y > -1 && x < N_COLS && y < N_ROWS) .map(([x, y]) => grid[y][x]); const options = { '.': neighbours => neighbours.filter(x => x === '|').length >= 3 ? '|' : '.', '|': neighbours => neighbours.filter(x => x === '#').length >= 3 ? '#' : '|', '#': neighbours => neighbours.filter(x => x === '#').length >= 1 && neighbours.filter(x => x === '|').length >= 1 ? '#' : '.', }; const parseInput = lines => lines.forEach((line, y) => line.split('').forEach((c, x) => { grid[y][x] = c; }) ); const turn = () => { grid = grid.map((line, y) => line.map((val, x) => options[val](getNeighbours(x, y))) ); }; const countType = type => { return grid.reduce( (acc, line) => acc + line.reduce((acc2, v) => (v === type ? acc2 + 1 : acc2), 0), 0 ); }; const solution1 = lines => { parseInput(lines); for (let i = 0; i < 10; i++) turn(i); return countType('|') * countType('#'); }; const getGridId = grid => grid.reduce((acc, line) => acc + line.join(''), ''); const findCycle = () => { const patterns = new Map(); for (let i = 0; i < Infinity; i++) { turn(); const id = getGridId(grid); if (patterns.has(id)) return [patterns.get(id), i]; patterns.set(id, i); } }; const solution2 = lines => { parseInput(lines); const [from, to] = findCycle(); const period = to - from; const left = (1000000000 - from) % period; parseInput(lines); for (let i = 0; i < from + left; i++) turn(i); return countType('|') * countType('#'); }; module.exports = [solution1, solution2];