Created
December 8, 2015 05:48
-
-
Save kevinkace/9fada4b0dcdc1032dd20 to your computer and use it in GitHub Desktop.
Advent of Code - 6b
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "use strict"; | |
| var input = require("./input.js"), | |
| g = [], | |
| gs = 1000, | |
| x, y, | |
| count = 0, | |
| gridlington = function(size) { | |
| var arr = []; | |
| for (var i = 0; i < size; i++) { | |
| arr[i] = 0; | |
| } | |
| return arr; | |
| }, | |
| control = function(state, command) { | |
| if(command === "turn on") { | |
| return state + 1; | |
| } | |
| if(command === "turn off") { | |
| return state > 0 ? state - 1 : 0; | |
| } | |
| if(command === "toggle") { | |
| return state + 2; | |
| } | |
| }; | |
| console.time("create"); | |
| for (x = 0; x < gs; x++) { | |
| g[x] = gridlington(gs); | |
| } | |
| console.timeEnd("create"); | |
| console.time("commands"); | |
| input.split("\n") | |
| .filter(function(input) { | |
| return input.length > 0; | |
| }) | |
| .forEach(function(inp) { | |
| var command = inp.match(/\D+/)[0].trim(), | |
| coords = inp.match(/[0-9]+,[0-9]+/g).map(function(coord){ | |
| return coord.split(",").map(function(c) { | |
| return parseInt(c, 10); | |
| }); | |
| }); | |
| for (var x = coords[0][0]; x <= coords[1][0]; x++) { | |
| for (var y = coords[0][1]; y <= coords[1][1]; y++) { | |
| g[x][y] = control(g[x][y], command); | |
| } | |
| } | |
| }); | |
| console.timeEnd("commands"); | |
| console.time("count"); | |
| for (x = 0; x < gs; x++) { | |
| for (y = 0; y < gs; y++) { | |
| count += g[x][y]; | |
| } | |
| } | |
| console.timeEnd("count"); | |
| console.log("count: " + count); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment