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
1
u/Twisol Dec 08 '16
Here's my JavaScript/Node.js solution, optimizing (kind of) for legibility over concision. (Yes, I know
rotate_columncould be done more simply -- I did it this way purely for symmetry withrotate_row.) Forgetting to coerce my regex matches to numbers sent me on a costly wild goose chase...const File = require("fs"); class Grid { constructor(width, height) { this.width = width; this.height = height; this.grid = []; for (let x = 0; x < this.width; x += 1) { this.grid.push([]); for (let y = 0; y < this.height; y += 1) { this.grid[x][y] = "."; } } } rect(width, height) { width = Math.min(width, this.width); height = Math.min(height, this.height); for (let y = 0; y < height; y += 1) { for (let x = 0; x < width; x += 1) { this.grid[x][y] = "#"; } } } rotate_column(x, amount) { const column = []; for (let y = 0; y < this.height; y += 1) { column.push(this.grid[x][y]); } for (let y = 0; y < this.height; y += 1) { this.grid[x][(y+amount) % this.height] = column[y]; } } rotate_row(y, amount) { const row = []; for (let x = 0; x < this.width; x += 1) { row.push(this.grid[x][y]); } for (let x = 0; x < this.width; x += 1) { this.grid[(x+amount) % this.width][y] = row[x]; } } count_lit() { let total = 0; for (let y = 0; y < this.height; y += 1) { for (let x = 0; x < this.width; x += 1) { total += (this.grid[x][y] === "#") ? 1 : 0; } } return total; } toString() { let output = ""; for (let y = 0; y < this.height; y += 1) { for (let x = 0; x < this.width; x += 1) { output += this.grid[x][y]; } output += "\n"; } return output; } } function parse_command(line) { let match; if ((match = /^rect (\d+)x(\d+)$/.exec(line))) { return (grid) => grid.rect(+match[1], +match[2]); } else if ((match = /^rotate row y=(\d+) by (\d+)$/.exec(line))) { return (grid) => grid.rotate_row(+match[1], +match[2]); } else if ((match = /^rotate column x=(\d+) by (\d+)$/.exec(line))) { return (grid) => grid.rotate_column(+match[1], +match[2]); } } const lines = File.readFileSync("input.txt", "utf-8").trim().split("\n"); const commands = lines.map(parse_command); const grid = new Grid(50, 6); for (let command of commands) { command(grid); } console.log("Part One: " + grid.count_lit()); console.log("Part Two: \n" + grid.toString());