this repo has no description
0
fork

Configure Feed

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

Day 6!

+599 -5
+33 -5
index.js
··· 6 6 */ 7 7 var Day1 = require('./lib/day1'); 8 8 var day1Input = fs.readFileSync('./input/day1.txt', 'utf8'); 9 - var day1Part1Answer = require('./lib/day1').floorCount(day1Input); 10 - var day1Part2Answer = require('./lib/day1').entersBasementAt(day1Input); 9 + 10 + var day1Part1Answer = Day1.floorCount(day1Input); 11 + var day1Part2Answer = Day1.entersBasementAt(day1Input); 12 + 11 13 console.log('Day 1, Part 1:', day1Part1Answer); 12 14 console.log('Day 1, Part 2:', day1Part2Answer); 13 15 14 - // TODO: Go back and redo days 1 - 3, for consistency :) 16 + // TODO: Go back and redo days 2 & 3, for consistency :) 15 17 16 - /** 18 + /* 17 19 * Day 4! 18 20 * ---------------------------------- 19 21 */ 20 22 var Day4 = require('./lib/day4'); 21 23 var day4Input = fs.readFileSync('./input/day4.txt', 'utf8').replace(/[^A-z0-9]/g, ''); 24 + 22 25 var day4Part1Answer = Day4.adventCoinSecret(day4Input, '00000'); 23 26 var day4Part2Answer = Day4.adventCoinSecret(day4Input, '000000'); 27 + 24 28 console.log('Day 4, Part 1:', day4Part1Answer); 25 29 console.log('Day 4, Part 2:', day4Part2Answer); 26 30 27 - /** 31 + /* 28 32 * Day 5! 29 33 * ---------------------------------- 30 34 */ 31 35 var Day5 = require('./lib/day5'); 32 36 var day5Input = fs.readFileSync('./input/day5.txt', 'utf8'); 37 + 33 38 var niceStringCount1 = 0; 34 39 var niceStringCount2 = 0; 40 + 35 41 day5Input.split('\n').forEach(function(str) { 36 42 if (Day5.isStringNice1(str)) niceStringCount1++; 37 43 if (Day5.isStringNice2(str)) niceStringCount2++; 38 44 }); 45 + 39 46 console.log('Day 5, Part 1:', niceStringCount1); 40 47 console.log('Day 5, Part 2:', niceStringCount2); 48 + 49 + /** 50 + * Day 6! 51 + * ---------------------------------- 52 + */ 53 + var Day6 = require('./lib/day6'); 54 + var day6Input = fs.readFileSync('./input/day6.txt', 'utf8'); 55 + 56 + var grid1 = Day6.buildGrid(1000, 1000); 57 + day6Input.split('\n').forEach(function(instruction) { 58 + grid1 = Day6.processInstruction1(grid1, instruction); 59 + }); 60 + var count = Day6.countLightsOn(grid1); 61 + console.log('Day 6, Part 1:', count); 62 + 63 + var grid2 = Day6.buildGrid(1000, 1000); 64 + day6Input.split('\n').forEach(function(instruction) { 65 + grid2 = Day6.processInstruction2(grid2, instruction); 66 + }); 67 + var totalBrightness = Day6.totalBrightness(grid2); 68 + console.log('Day 6, Part 2:', totalBrightness);
+300
input/day6.txt
··· 1 + turn on 489,959 through 759,964 2 + turn off 820,516 through 871,914 3 + turn off 427,423 through 929,502 4 + turn on 774,14 through 977,877 5 + turn on 410,146 through 864,337 6 + turn on 931,331 through 939,812 7 + turn off 756,53 through 923,339 8 + turn off 313,787 through 545,979 9 + turn off 12,823 through 102,934 10 + toggle 756,965 through 812,992 11 + turn off 743,684 through 789,958 12 + toggle 120,314 through 745,489 13 + toggle 692,845 through 866,994 14 + turn off 587,176 through 850,273 15 + turn off 674,321 through 793,388 16 + toggle 749,672 through 973,965 17 + turn on 943,30 through 990,907 18 + turn on 296,50 through 729,664 19 + turn on 212,957 through 490,987 20 + toggle 171,31 through 688,88 21 + turn off 991,989 through 994,998 22 + turn off 913,943 through 958,953 23 + turn off 278,258 through 367,386 24 + toggle 275,796 through 493,971 25 + turn off 70,873 through 798,923 26 + toggle 258,985 through 663,998 27 + turn on 601,259 through 831,486 28 + turn off 914,94 through 941,102 29 + turn off 558,161 through 994,647 30 + turn on 119,662 through 760,838 31 + toggle 378,775 through 526,852 32 + turn off 384,670 through 674,972 33 + turn off 249,41 through 270,936 34 + turn on 614,742 through 769,780 35 + turn on 427,70 through 575,441 36 + turn on 410,478 through 985,753 37 + turn off 619,46 through 931,342 38 + turn on 284,55 through 768,922 39 + turn off 40,592 through 728,685 40 + turn on 825,291 through 956,950 41 + turn on 147,843 through 592,909 42 + turn off 218,675 through 972,911 43 + toggle 249,291 through 350,960 44 + turn off 556,80 through 967,675 45 + toggle 609,148 through 968,279 46 + toggle 217,605 through 961,862 47 + toggle 407,177 through 548,910 48 + toggle 400,936 through 599,938 49 + turn off 721,101 through 925,455 50 + turn on 268,631 through 735,814 51 + toggle 549,969 through 612,991 52 + toggle 553,268 through 689,432 53 + turn off 817,668 through 889,897 54 + toggle 801,544 through 858,556 55 + toggle 615,729 through 832,951 56 + turn off 427,477 through 958,948 57 + turn on 164,49 through 852,946 58 + turn on 542,449 through 774,776 59 + turn off 923,196 through 980,446 60 + toggle 90,310 through 718,846 61 + turn off 657,215 through 744,252 62 + turn off 800,239 through 811,712 63 + turn on 502,90 through 619,760 64 + toggle 649,512 through 862,844 65 + turn off 334,903 through 823,935 66 + turn off 630,233 through 839,445 67 + turn on 713,67 through 839,865 68 + turn on 932,50 through 982,411 69 + turn off 480,729 through 984,910 70 + turn on 100,219 through 796,395 71 + turn on 758,108 through 850,950 72 + turn off 427,276 through 439,938 73 + turn on 178,284 through 670,536 74 + toggle 540,27 through 625,102 75 + turn off 906,722 through 936,948 76 + toggle 345,418 through 859,627 77 + toggle 175,775 through 580,781 78 + toggle 863,28 through 929,735 79 + turn off 824,858 through 905,973 80 + toggle 752,312 through 863,425 81 + turn on 985,716 through 988,852 82 + turn off 68,504 through 763,745 83 + toggle 76,209 through 810,720 84 + turn off 657,607 through 676,664 85 + toggle 596,869 through 896,921 86 + turn off 915,411 through 968,945 87 + turn off 368,39 through 902,986 88 + turn on 11,549 through 393,597 89 + turn off 842,893 through 976,911 90 + toggle 274,106 through 581,329 91 + toggle 406,403 through 780,950 92 + toggle 408,988 through 500,994 93 + toggle 217,73 through 826,951 94 + turn on 917,872 through 961,911 95 + toggle 394,34 through 510,572 96 + toggle 424,603 through 583,626 97 + toggle 106,159 through 755,738 98 + turn off 244,610 through 472,709 99 + turn on 350,265 through 884,690 100 + turn on 688,184 through 928,280 101 + toggle 279,443 through 720,797 102 + turn off 615,493 through 888,610 103 + toggle 118,413 through 736,632 104 + turn on 798,782 through 829,813 105 + turn off 250,934 through 442,972 106 + turn on 68,503 through 400,949 107 + toggle 297,482 through 313,871 108 + toggle 710,3 through 839,859 109 + turn on 125,300 through 546,888 110 + toggle 482,39 through 584,159 111 + turn off 536,89 through 765,962 112 + turn on 530,518 through 843,676 113 + turn on 994,467 through 994,676 114 + turn on 623,628 through 744,927 115 + toggle 704,912 through 837,983 116 + turn on 154,364 through 517,412 117 + toggle 344,409 through 780,524 118 + turn off 578,740 through 725,879 119 + turn on 251,933 through 632,957 120 + turn on 827,705 through 971,789 121 + toggle 191,282 through 470,929 122 + toggle 324,525 through 446,867 123 + toggle 534,343 through 874,971 124 + toggle 550,650 through 633,980 125 + toggle 837,404 through 881,915 126 + toggle 338,881 through 845,905 127 + turn on 469,462 through 750,696 128 + turn on 741,703 through 892,870 129 + turn off 570,215 through 733,562 130 + turn on 445,576 through 870,775 131 + turn on 466,747 through 554,878 132 + turn off 820,453 through 868,712 133 + turn off 892,706 through 938,792 134 + turn off 300,238 through 894,746 135 + turn off 306,44 through 457,444 136 + turn off 912,569 through 967,963 137 + toggle 109,756 through 297,867 138 + turn on 37,546 through 41,951 139 + turn on 321,637 through 790,910 140 + toggle 66,50 through 579,301 141 + toggle 933,221 through 933,791 142 + turn on 486,676 through 878,797 143 + turn on 417,231 through 556,317 144 + toggle 904,468 through 981,873 145 + turn on 417,675 through 749,712 146 + turn on 692,371 through 821,842 147 + toggle 324,73 through 830,543 148 + turn on 912,490 through 977,757 149 + turn off 634,872 through 902,949 150 + toggle 266,779 through 870,798 151 + turn on 772,982 through 990,996 152 + turn off 607,46 through 798,559 153 + turn on 295,602 through 963,987 154 + turn on 657,86 through 944,742 155 + turn off 334,639 through 456,821 156 + turn off 997,667 through 997,670 157 + turn off 725,832 through 951,945 158 + turn off 30,120 through 952,984 159 + turn on 860,965 through 917,976 160 + toggle 471,997 through 840,998 161 + turn off 319,307 through 928,504 162 + toggle 823,631 through 940,908 163 + toggle 969,984 through 981,993 164 + turn off 691,319 through 865,954 165 + toggle 911,926 through 938,929 166 + turn on 953,937 through 968,991 167 + toggle 914,643 through 975,840 168 + turn on 266,982 through 436,996 169 + turn off 101,896 through 321,932 170 + turn off 193,852 through 751,885 171 + turn off 576,532 through 863,684 172 + turn on 761,456 through 940,783 173 + turn on 20,290 through 398,933 174 + turn off 435,335 through 644,652 175 + turn on 830,569 through 905,770 176 + turn off 630,517 through 905,654 177 + turn on 664,53 through 886,976 178 + toggle 275,416 through 408,719 179 + turn on 370,621 through 515,793 180 + turn on 483,373 through 654,749 181 + turn on 656,786 through 847,928 182 + turn off 532,752 through 945,974 183 + toggle 301,150 through 880,792 184 + turn off 951,488 through 958,952 185 + turn on 207,729 through 882,828 186 + toggle 694,532 through 973,961 187 + toggle 676,639 through 891,802 188 + turn off 653,6 through 905,519 189 + toggle 391,109 through 418,312 190 + turn on 877,423 through 957,932 191 + turn on 340,145 through 563,522 192 + turn off 978,467 through 988,895 193 + turn off 396,418 through 420,885 194 + turn off 31,308 through 816,316 195 + turn on 107,675 through 758,824 196 + turn on 61,82 through 789,876 197 + turn on 750,743 through 754,760 198 + toggle 88,733 through 736,968 199 + turn off 754,349 through 849,897 200 + toggle 157,50 through 975,781 201 + turn off 230,231 through 865,842 202 + turn off 516,317 through 630,329 203 + turn off 697,820 through 829,903 204 + turn on 218,250 through 271,732 205 + toggle 56,167 through 404,431 206 + toggle 626,891 through 680,927 207 + toggle 370,207 through 791,514 208 + toggle 860,74 through 949,888 209 + turn on 416,527 through 616,541 210 + turn off 745,449 through 786,908 211 + turn on 485,554 through 689,689 212 + turn on 586,62 through 693,141 213 + toggle 506,759 through 768,829 214 + turn on 473,109 through 929,166 215 + turn on 760,617 through 773,789 216 + toggle 595,683 through 618,789 217 + turn off 210,775 through 825,972 218 + toggle 12,426 through 179,982 219 + turn on 774,539 through 778,786 220 + turn on 102,498 through 121,807 221 + turn off 706,897 through 834,965 222 + turn off 678,529 through 824,627 223 + turn on 7,765 through 615,870 224 + turn off 730,872 through 974,943 225 + turn off 595,626 through 836,711 226 + turn off 215,424 through 841,959 227 + toggle 341,780 through 861,813 228 + toggle 507,503 through 568,822 229 + turn on 252,603 through 349,655 230 + toggle 93,521 through 154,834 231 + turn on 565,682 through 951,954 232 + turn on 544,318 through 703,418 233 + toggle 756,953 through 891,964 234 + turn on 531,123 through 856,991 235 + turn on 148,315 through 776,559 236 + turn off 925,835 through 963,971 237 + turn on 895,944 through 967,964 238 + turn off 102,527 through 650,747 239 + toggle 626,105 through 738,720 240 + turn off 160,75 through 384,922 241 + toggle 813,724 through 903,941 242 + turn on 207,107 through 982,849 243 + toggle 750,505 through 961,697 244 + toggle 105,410 through 885,819 245 + turn on 226,104 through 298,283 246 + turn off 224,604 through 508,762 247 + turn on 477,368 through 523,506 248 + turn off 477,901 through 627,936 249 + turn off 887,131 through 889,670 250 + turn on 896,994 through 938,999 251 + toggle 401,580 through 493,728 252 + toggle 987,184 through 991,205 253 + turn on 821,643 through 882,674 254 + toggle 784,940 through 968,959 255 + turn off 251,293 through 274,632 256 + turn off 339,840 through 341,844 257 + turn off 675,351 through 675,836 258 + toggle 918,857 through 944,886 259 + toggle 70,253 through 918,736 260 + turn off 612,604 through 772,680 261 + turn off 277,40 through 828,348 262 + toggle 692,139 through 698,880 263 + toggle 124,446 through 883,453 264 + toggle 969,932 through 990,945 265 + toggle 855,692 through 993,693 266 + toggle 722,472 through 887,899 267 + toggle 978,149 through 985,442 268 + toggle 837,540 through 916,889 269 + turn off 612,2 through 835,82 270 + toggle 560,767 through 878,856 271 + turn on 461,734 through 524,991 272 + toggle 206,824 through 976,912 273 + turn on 826,610 through 879,892 274 + turn on 577,699 through 956,933 275 + turn off 9,250 through 50,529 276 + turn off 77,657 through 817,677 277 + turn on 68,419 through 86,426 278 + turn on 991,720 through 992,784 279 + turn on 668,20 through 935,470 280 + turn off 133,418 through 613,458 281 + turn off 487,286 through 540,328 282 + toggle 247,874 through 840,955 283 + toggle 301,808 through 754,970 284 + turn off 34,194 through 578,203 285 + turn off 451,49 through 492,921 286 + turn on 907,256 through 912,737 287 + turn off 479,305 through 702,587 288 + turn on 545,583 through 732,749 289 + toggle 11,16 through 725,868 290 + turn on 965,343 through 986,908 291 + turn on 674,953 through 820,965 292 + toggle 398,147 through 504,583 293 + turn off 778,194 through 898,298 294 + turn on 179,140 through 350,852 295 + turn off 241,118 through 530,832 296 + turn off 41,447 through 932,737 297 + turn off 820,663 through 832,982 298 + turn on 550,460 through 964,782 299 + turn on 31,760 through 655,892 300 + toggle 628,958 through 811,992
+120
lib/day6.js
··· 1 + var R_PARSE_INSTRUCTION = /^(.+)\s(\d+),(\d+)\sthrough\s(\d+),(\d+)/; 2 + 3 + module.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 + 97 + function 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 + 111 + function 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 + }
+146
spec/day6.spec.js
··· 1 + var expect = require('chai').expect; 2 + var Day6 = require('../lib/day6'); 3 + 4 + describe('Day6', function() { 5 + var grid; 6 + 7 + beforeEach(function() { 8 + grid = Day6.buildGrid(2, 3); 9 + }); 10 + 11 + describe('#buildGrid', function() { 12 + it('builds a grid of booleans from specified dimensions', function() { 13 + expect(grid).to.eql([ 14 + [0, 0], 15 + [0, 0], 16 + [0, 0] 17 + ]); 18 + }); 19 + }); 20 + 21 + describe('#turnOn', function() { 22 + it('makes sure lights are ON in specified range', function() { 23 + grid = Day6.turnOn(grid, [0,0], [1,1]); 24 + expect(grid).to.eql([ 25 + [1, 1], 26 + [1, 1], 27 + [0, 0] 28 + ]); 29 + }); 30 + }); 31 + 32 + describe('#turnOff', function() { 33 + it('makes sure lights are OFF in specified range', function() { 34 + grid = Day6.turnOn(grid, [0,0], [1,1]); 35 + grid = Day6.turnOff(grid, [0,1], [1,1]); 36 + 37 + expect(grid).to.eql([ 38 + [1, 0], 39 + [1, 0], 40 + [0, 0] 41 + ]); 42 + }); 43 + }); 44 + 45 + describe('#toggle', function() { 46 + it('TOGGLES the lights at specified range', function() { 47 + grid = Day6.turnOn(grid, [0,0], [1,1]); 48 + grid = Day6.toggle(grid, [0,1], [1,1]); 49 + 50 + expect(grid).to.eql([ 51 + [1, 0], 52 + [1, 0], 53 + [0, 0] 54 + ]); 55 + }); 56 + }); 57 + 58 + describe('#reduceBrightness', function() { 59 + it('reduces the brightness of specified range by 1, to a min of 0', function() { 60 + grid = Day6.toggle(grid, [0,0], [2,1]); 61 + grid = Day6.reduceBrightness(grid, [0,0], [0,1]); 62 + grid = Day6.reduceBrightness(grid, [0,0], [0,1]); 63 + 64 + expect(grid).to.eql([ 65 + [0, 0], 66 + [1, 1], 67 + [1, 1] 68 + ]); 69 + }); 70 + }); 71 + 72 + describe('#increaseBrightness', function() { 73 + it('increase the brightness of a specified range by 1', function() { 74 + grid = Day6.toggle(grid, [0,0], [2,1]); 75 + grid = Day6.increaseBrightness(grid, [0,0], [0,1]); 76 + 77 + expect(grid).to.eql([ 78 + [2, 2], 79 + [1, 1], 80 + [1, 1] 81 + ]); 82 + }); 83 + }); 84 + 85 + describe('#totalBrightness', function() { 86 + it('calculates the total brightness of all lights in the grid', function() { 87 + grid = Day6.toggle(grid, [0,0], [2,1]); 88 + 89 + expect(grid).to.eql([ 90 + [1, 1], 91 + [1, 1], 92 + [1, 1] 93 + ]); 94 + 95 + expect(Day6.totalBrightness(grid)).to.equal(6); 96 + }); 97 + }); 98 + 99 + describe('#processInstruction1', function() { 100 + it('turns a text instruction into a command with to and from coords', function() { 101 + var instructions = [ 102 + 'turn on 0,0 through 1,1', 103 + 'turn off 0,1 through 1,1', 104 + 'toggle 0,0 through 2,0' 105 + ]; 106 + 107 + instructions.forEach(function(instruction) { 108 + grid = Day6.processInstruction1(grid, instruction); 109 + }); 110 + 111 + expect(grid).to.eql([ 112 + [0, 0], 113 + [0, 0], 114 + [1, 0] 115 + ]); 116 + }); 117 + }); 118 + 119 + describe('#processInstruction2', function() { 120 + it('turns a text instruction into a command with to and from coords', function() { 121 + var instructions = [ 122 + 'turn off 0,0 through 2,1', 123 + 'turn on 0,0 through 1,1', 124 + 'turn off 0,1 through 1,1', 125 + 'toggle 0,0 through 2,0' 126 + ]; 127 + 128 + instructions.forEach(function(instruction) { 129 + grid = Day6.processInstruction2(grid, instruction); 130 + }); 131 + 132 + expect(grid).to.eql([ 133 + [3, 0], 134 + [3, 0], 135 + [2, 0] 136 + ]); 137 + }); 138 + }); 139 + 140 + describe('#countLightsOn', function() { 141 + it('returns the number of lights that are on', function() { 142 + grid = Day6.turnOn(grid, [0,0], [1,0]); 143 + expect(Day6.countLightsOn(grid)).to.equal(2); 144 + }); 145 + }); 146 + });