Skip to content

Instantly share code, notes, and snippets.

@TristanWiley
Created December 8, 2016 17:41
Show Gist options
  • Select an option

  • Save TristanWiley/5c430165d1d9a02f72b34016b527fa5f to your computer and use it in GitHub Desktop.

Select an option

Save TristanWiley/5c430165d1d9a02f72b34016b527fa5f to your computer and use it in GitHub Desktop.
String.prototype.replaceAt = function(index, character) {
return this.substr(0, index) + character + this.substr(index + character.length);
}
var instructions = document.body.textContent.split('\n');
var grid = [];
//create grid
for (var a = 0; a < 6; a++) {
grid.push(Array(51).join("0"));
}
instructions.forEach(function(item) {
if (item.split(' ')[0] == "rotate") {
if (item.split(" ")[1] == "row") {
var row = Number(item.charAt(item.indexOf("=") + 1));
var dis = Number(item.split(" ")[item.split(" ").length - 1]);
shiftRowRight(row, dis);
} else {
var row = Number(item.charAt(item.indexOf("=") + 1));
var dis = Number(item.split(" ")[item.split(" ").length - 1]);
shiftRowDown(row, dis);
}
} else {
if (item) {
var p1 = Number(item.split(' ')[1].split('x')[0]);
var p2 = Number(item.split(' ')[1].split('x')[1]);
makeRect(p1, p2);
}
}
});
function makeRect(w, h) {
for (var b = 0; b < h; b++) {
for (var c = 0; c < w; c++) {
grid[b] = grid[b].replaceAt(c, "1");
}
}
}
function shiftRowRight(row, dis) {
var ar = grid[row].split('');
ar = ar.concat(ar.splice(0, ar.length - dis));
grid[row] = ar.join("");
}
function shiftRowDown(col, dis) {
var arr = [];
grid.forEach(function(row) {
arr.push(row.charAt(col));
});
arr = arr.concat(arr.splice(0, arr.length - dis));
for (var d = 0; d < grid.length; d++) {
grid[d] = grid[d].replaceAt(col, arr[d]);
}
}
//find answer
var answer = 0;
grid.forEach(function(finalRow) {
answer += (finalRow.match(/1/g) || []).length;
});
console.log(grid.toString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment