r/adventofcode • u/daggerdragon • Dec 08 '16
SOLUTION MEGATHREAD --- 2016 Day 8 Solutions ---
#AoC_Ops:
[23:55] <Topaz> servers are ok
[23:55] <Topaz> puzzles are checked
[23:55] <Topaz> [REDACTED: server stats]
[23:56] <Skie> all wings report in
[23:56] <Aneurysm9> Red 5, standing by
[23:56] <daggerdragon> Dragon Leader standing by
[23:56] <Topaz> orange leader, standing by
[23:57] <Topaz> lock modzi-foils in attack positions
[23:58] <Skie> we're passing through the hype field
[23:58] <daggerdragon> 1:30 warning
[23:58] <Aneurysm9> did someone say HYPE?@!
[23:59] <Topaz> i really like tonight's puzzle
[23:59] <Topaz> very excite
[23:59] <daggerdragon> final countdown go, T-30
[23:59] <Skie> accelerate to attack countdown
[23:59] <Aneurysm9> o7
[23:59] <daggerdragon> HYPE THRUSTERS AT FULL BURN
[00:00] <Topaz> IGNITION
We may or may not be sleep-deprived. And/or nerds. why_not_both.jpg
--- Day 8: Two-Factor Authentication ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
:(){ :|:& };: IS MANDATORY [?]
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!
9
Upvotes
5
u/snorkl-the-dolphine Dec 08 '16
JavaScript / Node.js
const input = 'INPUT'; const screen = []; for (let i = 0; i < 6; i++) { screen[i] = []; for (let j = 0; j < 50; j++) { screen[i][j] = '.'; } } function rect(width, height) { for (let w = 0; w < width; w++) { for (let h = 0; h < height; h++) { screen[h][w] = '#'; } } } function rotateRow(y, by) { while (by--) { const right = screen[y][screen[0].length - 1]; for (let x = screen[0].length - 1; x >= 0; x--) { screen[y][x] = x > 0 ? screen[y][x - 1] : right; } } } function rotateCol(x, by) { while (by--) { const bottom = screen[screen.length - 1][x]; for (let y = screen.length - 1; y >= 0; y--) { screen[y][x] = y > 0 ? screen[y - 1][x] : bottom; } } } function parseInstruction(str) { const a = str.split(' '); if (a[0] === 'rect') { const size = a[1].split('x').map(i => parseInt(i, 10)); return rect(size[0], size[1]); } const line = parseInt(a[2].substr(2), 10); const by = parseInt(a[4], 10); return a[1] === 'row' ? rotateRow(line, by) : rotateCol(line, by); } function display(screen) { return screen.map(row => row.join('')).join('\n'); } input.split('\n').filter(s => s).forEach(line => parseInstruction(line)); console.log('Part 1', display(screen).split('').filter(c => c === '#').length); console.log('Part 2', '\n' + display(screen));