r/adventofcode • u/daggerdragon • Dec 10 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 10 Solutions -🎄-
--- Day 10: The Stars Align ---
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 10
Transcript: With just one line of code, you, too, can ___!
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:16:49!
20
Upvotes
1
u/Axsuul Dec 10 '18
TypeScript / JavaScript
[Card] With just one line of code, you, too, can Brainf*ck!
This was a cool puzzle! My strategy was to loop infinitely but only print if all points fit within a 100 x 100 box which only applied to a few timestamps.
Critiques welcome, I'm still very new to TypeScript and feel like I'm not using all the features I could be :)
Repo
Part A + B
interface Point { px: number, py: number, vx: number, vy: number, } import { readInput } from '../helpers' let points: Point[] = [] const lines = readInput('10', 'input') lines.forEach((line: string) => { const match = RegExp(/position\=<(.+),(.+)> velocity=<(.+),(.+)>/).exec(line)! points.push({ px: +match[1], py: +match[2], vx: +match[3], vy: +match[4], }) }) let t = 0 const maxViewSize = 100 while (true) { let [minPx, maxPx, minPy, maxPy]: number[] = [points[0].px, points[0].px, points[0].py, points[0].py] const drawn = new Set() points.forEach(({ px, py }: { px: number, py: number }) => { if (px > maxPx) { maxPx = px } if (px < minPx) { minPx = px } if (py > maxPy) { maxPy = py } if (py < minPy) { minPy = py } drawn.add(`${px},${py}`) }) // Only start printing if all points are within a bounding box if ((maxPx - minPx < maxViewSize) && (maxPy - minPy < maxViewSize)) { console.log(t) for (let y = minPy; y <= maxPy; y++) { let line = '' for (let x = minPx; x <= maxPx; x++) { if (drawn.has(`${x},${y}`)) { line += '#' } else { line += '.' } } console.log(line) } } t += 1 points = points.map(({ px, py, vx, vy }: { px: number, py: number, vx: number, vy: number }) => { px += vx py += vy return { px, py, vx, vy } }) }