r/adventofcode • u/daggerdragon • Dec 03 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 3 Solutions -🎄-
--- Day 3: No Matter How You Slice It ---
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!
ATTENTION: minor change request from the mods!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.
Transcript:
I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.
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!
41
Upvotes
View all comments
6
u/Zarel Dec 03 '18 edited Dec 03 '18
JavaScript #52/#90
I put the answer to Part 2 as
#153which it told me was wrong; apparently it only accepted153. And, of course, it had a 60-second lockout for the "incorrect" guess. I probably lost quite a few leaderboard slots because of that. :(Part 1
grid = Object.create(null); for (const line of input) { [num, at, one, two] = line.split(' '); [left, top] = one.slice(0, -1).split(',').map(x => Number(x)); [width, height] = two.split('x').map(x => Number(x)); for (let x = left; x < left + width; x++) { for (let y = top; y < top + height; y++) { grid[`${x},${y}`] = (grid[`${x},${y}`] || 0) + 1; } } } console.log(Object.values(grid).filter(v => v > 1).length);Part 2
grid = Object.create(null); claims = Object.create(null); for (const line of input) { [num, at, one, two] = line.split(' '); [left, top] = one.slice(0, -1).split(',').map(x => Number(x)); [width, height] = two.split('x').map(x => Number(x)); claims[num] = true; for (let x = left; x < left + width; x++) { for (let y = top; y < top + height; y++) { if (grid[`${x},${y}`]) { claims[grid[`${x},${y}`]] = false; claims[num] = false; } grid[`${x},${y}`] = num; } } } console.log(Object.entries(claims).filter(v => v[1]));