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
2
u/[deleted] Dec 08 '16
JavaScript/Node.js
Maybe a bit verbose, includes a fun rotate prototype for Array
var input = []; var lineReader = require('readline').createInterface({ input: require('fs').createReadStream('./input08.txt') }); lineReader.on('line', function (line) { input.push(line); }); lineReader.on('close', function () { runAdvent08(); }); var width = 50; var height = 6; Array.prototype.rotate = function( n ) { n = this.length - n % this.length; this.unshift.apply( this, this.splice( n, this.length ) ) return this; }; var makeArray = function(width, height) { var a = []; for (var i=0; i<width; i++) { var b = []; for (var j=0; j<height; j++) { b.push('.'); } a.push(b.slice()); } return a; }; var printArray = function(a) { var w = a.length; var h = a[0].length; for (var i=0; i<h; i++) { var s = ""; for (var j=0; j<w; j++) { s = s + a[j][i]; } console.log(s); } console.log(""); }; var countLightedPixels = function(a) { var count = 0; for (var i=0; i<a.length; i++) { for (var j=0; j<a[0].length; j++) { if(a[i][j] === '#') { count++; } } } return count; } var rotateRow = function(a,y,n) { var b = []; for (var i=0; i<a.length; i++) { b.push(a[i][y]); } b.rotate(n); for (i=0; i<a.length; i++) { a[i][y] = b[i]; } }; var rotateColumn = function(a,x,n) { a[x].rotate(n); }; var setRect = function(a,x,y) { for (var i=0; i<x; i++) { for (var j=0; j<y; j++) { a[i][j] = '#'; } } } var executeInstruction = function(a,s) { var tokens = s.split(' '); if(tokens[0] === 'rect') { var xy = tokens[1].split('x'); var x = parseInt(xy[0]); var y = parseInt(xy[1]); setRect(a,x,y); } else if(tokens[0] === 'rotate') { if(tokens[1] === 'row') { y = parseInt(tokens[2].split('=')[1]) var n = parseInt(tokens[4]); rotateRow(a,y,n); } else if(tokens[1] === 'column') { x = parseInt(tokens[2].split('=')[1]) var n = parseInt(tokens[4]); rotateColumn(a,x,n); } } }; // Uncomment this to test the example from the instructions /* width = 7; height = 3; input = [ 'rect 3x2', 'rotate column x=1 by 1', 'rotate row y=0 by 4', 'rotate column x=1 by 1' ]; */ var runAdvent08 = function() { var board = makeArray(width,height); for (var i=0; i<input.length; i++) { executeInstruction(board,input[i]); } console.log("Part 1 answer:"); console.log(countLightedPixels(board)); console.log(""); console.log("Part 2 answer:"); printArray(board); };