this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 120 lines 2.9 kB view raw
1var R_PARSE_INSTRUCTION = /^(.+)\s(\d+),(\d+)\sthrough\s(\d+),(\d+)/; 2 3module.exports = { 4 5 processInstruction1: function(grid, instruction) { 6 var parts = instruction.match(R_PARSE_INSTRUCTION); 7 8 if (!parts) return grid; // Invalid instruction 9 10 var command = parts[1]; 11 var from = [parseInt(parts[2]), parseInt(parts[3])]; 12 var to = [parseInt(parts[4]), parseInt(parts[5])]; 13 14 switch (command) { 15 case 'turn on': return this.turnOn(grid, from, to); 16 case 'turn off': return this.turnOff(grid, from, to); 17 case 'toggle': return this.toggle(grid, from, to); 18 } 19 }, 20 21 processInstruction2: function(grid, instruction) { 22 var parts = instruction.match(R_PARSE_INSTRUCTION); 23 24 if (!parts) return grid; // Invalid instruction 25 26 var command = parts[1]; 27 var from = [parseInt(parts[2]), parseInt(parts[3])]; 28 var to = [parseInt(parts[4]), parseInt(parts[5])]; 29 30 switch (command) { 31 case 'turn on': return this.increaseBrightness(grid, from, to); 32 case 'turn off': return this.reduceBrightness(grid, from, to); 33 case 'toggle': 34 return this.increaseBrightness( 35 this.increaseBrightness(grid, from, to), 36 from, to); 37 } 38 }, 39 40 buildGrid: function(x, y) { 41 var grid = []; 42 43 for (var i = 0; i < y; i++) { 44 grid.push([]); 45 } 46 47 grid.forEach(function(row) { 48 for (var i = 0; i < x; i++) { 49 row.push(0); 50 } 51 }); 52 53 return grid; 54 }, 55 56 turnOn: function(grid, from, to) { 57 return setGridValues(grid, from, to, 1); 58 }, 59 turnOff: function(grid, from, to) { 60 return setGridValues(grid, from, to, 0); 61 }, 62 toggle: function(grid, from, to) { 63 return setGridValues(grid, from, to, 'toggle'); 64 }, 65 increaseBrightness: function(grid, from, to) { 66 return changeGridValues(grid, from, to, 1); 67 }, 68 reduceBrightness: function(grid, from, to) { 69 return changeGridValues(grid, from, to, -1); 70 }, 71 72 countLightsOn: function(grid) { 73 var count = 0; 74 75 grid.forEach(function(row) { 76 row.forEach(function(light) { 77 if (light) count++; 78 }); 79 }); 80 81 return count; 82 }, 83 84 totalBrightness: function(grid) { 85 var total = 0; 86 87 grid.forEach(function(row) { 88 row.forEach(function(light) { 89 total += light; 90 }); 91 }); 92 93 return total; 94 } 95}; 96 97function setGridValues(grid, from, to, value) { 98 var newGrid = grid; 99 for (var i = from[0]; i <= to[0]; i++) { 100 for (var j = from[1]; j <= to[1]; j++) { 101 if (value === 'toggle') { 102 newGrid[i][j] = grid[i][j] === 0 ? 1 : 0; 103 } else { 104 newGrid[i][j] = value; 105 } 106 } 107 } 108 return grid; 109} 110 111function changeGridValues(grid, from, to, value) { 112 var newGrid = grid; 113 for (var i = from[0]; i <= to[0]; i++) { 114 for (var j = from[1]; j <= to[1]; j++) { 115 newGrid[i][j] = grid[i][j] + (value); 116 if (newGrid[i][j] < 0) newGrid[i][j] = 0; 117 } 118 } 119 return grid; 120}