···66 */
77var Day1 = require('./lib/day1');
88var day1Input = fs.readFileSync('./input/day1.txt', 'utf8');
99-var day1Part1Answer = require('./lib/day1').floorCount(day1Input);
1010-var day1Part2Answer = require('./lib/day1').entersBasementAt(day1Input);
99+1010+var day1Part1Answer = Day1.floorCount(day1Input);
1111+var day1Part2Answer = Day1.entersBasementAt(day1Input);
1212+1113console.log('Day 1, Part 1:', day1Part1Answer);
1214console.log('Day 1, Part 2:', day1Part2Answer);
13151414-// TODO: Go back and redo days 1 - 3, for consistency :)
1616+// TODO: Go back and redo days 2 & 3, for consistency :)
15171616-/**
1818+/*
1719 * Day 4!
1820 * ----------------------------------
1921 */
2022var Day4 = require('./lib/day4');
2123var day4Input = fs.readFileSync('./input/day4.txt', 'utf8').replace(/[^A-z0-9]/g, '');
2424+2225var day4Part1Answer = Day4.adventCoinSecret(day4Input, '00000');
2326var day4Part2Answer = Day4.adventCoinSecret(day4Input, '000000');
2727+2428console.log('Day 4, Part 1:', day4Part1Answer);
2529console.log('Day 4, Part 2:', day4Part2Answer);
26302727-/**
3131+/*
2832 * Day 5!
2933 * ----------------------------------
3034 */
3135var Day5 = require('./lib/day5');
3236var day5Input = fs.readFileSync('./input/day5.txt', 'utf8');
3737+3338var niceStringCount1 = 0;
3439var niceStringCount2 = 0;
4040+3541day5Input.split('\n').forEach(function(str) {
3642 if (Day5.isStringNice1(str)) niceStringCount1++;
3743 if (Day5.isStringNice2(str)) niceStringCount2++;
3844});
4545+3946console.log('Day 5, Part 1:', niceStringCount1);
4047console.log('Day 5, Part 2:', niceStringCount2);
4848+4949+/**
5050+ * Day 6!
5151+ * ----------------------------------
5252+ */
5353+var Day6 = require('./lib/day6');
5454+var day6Input = fs.readFileSync('./input/day6.txt', 'utf8');
5555+5656+var grid1 = Day6.buildGrid(1000, 1000);
5757+day6Input.split('\n').forEach(function(instruction) {
5858+ grid1 = Day6.processInstruction1(grid1, instruction);
5959+});
6060+var count = Day6.countLightsOn(grid1);
6161+console.log('Day 6, Part 1:', count);
6262+6363+var grid2 = Day6.buildGrid(1000, 1000);
6464+day6Input.split('\n').forEach(function(instruction) {
6565+ grid2 = Day6.processInstruction2(grid2, instruction);
6666+});
6767+var totalBrightness = Day6.totalBrightness(grid2);
6868+console.log('Day 6, Part 2:', totalBrightness);
+300
input/day6.txt
···11+turn on 489,959 through 759,964
22+turn off 820,516 through 871,914
33+turn off 427,423 through 929,502
44+turn on 774,14 through 977,877
55+turn on 410,146 through 864,337
66+turn on 931,331 through 939,812
77+turn off 756,53 through 923,339
88+turn off 313,787 through 545,979
99+turn off 12,823 through 102,934
1010+toggle 756,965 through 812,992
1111+turn off 743,684 through 789,958
1212+toggle 120,314 through 745,489
1313+toggle 692,845 through 866,994
1414+turn off 587,176 through 850,273
1515+turn off 674,321 through 793,388
1616+toggle 749,672 through 973,965
1717+turn on 943,30 through 990,907
1818+turn on 296,50 through 729,664
1919+turn on 212,957 through 490,987
2020+toggle 171,31 through 688,88
2121+turn off 991,989 through 994,998
2222+turn off 913,943 through 958,953
2323+turn off 278,258 through 367,386
2424+toggle 275,796 through 493,971
2525+turn off 70,873 through 798,923
2626+toggle 258,985 through 663,998
2727+turn on 601,259 through 831,486
2828+turn off 914,94 through 941,102
2929+turn off 558,161 through 994,647
3030+turn on 119,662 through 760,838
3131+toggle 378,775 through 526,852
3232+turn off 384,670 through 674,972
3333+turn off 249,41 through 270,936
3434+turn on 614,742 through 769,780
3535+turn on 427,70 through 575,441
3636+turn on 410,478 through 985,753
3737+turn off 619,46 through 931,342
3838+turn on 284,55 through 768,922
3939+turn off 40,592 through 728,685
4040+turn on 825,291 through 956,950
4141+turn on 147,843 through 592,909
4242+turn off 218,675 through 972,911
4343+toggle 249,291 through 350,960
4444+turn off 556,80 through 967,675
4545+toggle 609,148 through 968,279
4646+toggle 217,605 through 961,862
4747+toggle 407,177 through 548,910
4848+toggle 400,936 through 599,938
4949+turn off 721,101 through 925,455
5050+turn on 268,631 through 735,814
5151+toggle 549,969 through 612,991
5252+toggle 553,268 through 689,432
5353+turn off 817,668 through 889,897
5454+toggle 801,544 through 858,556
5555+toggle 615,729 through 832,951
5656+turn off 427,477 through 958,948
5757+turn on 164,49 through 852,946
5858+turn on 542,449 through 774,776
5959+turn off 923,196 through 980,446
6060+toggle 90,310 through 718,846
6161+turn off 657,215 through 744,252
6262+turn off 800,239 through 811,712
6363+turn on 502,90 through 619,760
6464+toggle 649,512 through 862,844
6565+turn off 334,903 through 823,935
6666+turn off 630,233 through 839,445
6767+turn on 713,67 through 839,865
6868+turn on 932,50 through 982,411
6969+turn off 480,729 through 984,910
7070+turn on 100,219 through 796,395
7171+turn on 758,108 through 850,950
7272+turn off 427,276 through 439,938
7373+turn on 178,284 through 670,536
7474+toggle 540,27 through 625,102
7575+turn off 906,722 through 936,948
7676+toggle 345,418 through 859,627
7777+toggle 175,775 through 580,781
7878+toggle 863,28 through 929,735
7979+turn off 824,858 through 905,973
8080+toggle 752,312 through 863,425
8181+turn on 985,716 through 988,852
8282+turn off 68,504 through 763,745
8383+toggle 76,209 through 810,720
8484+turn off 657,607 through 676,664
8585+toggle 596,869 through 896,921
8686+turn off 915,411 through 968,945
8787+turn off 368,39 through 902,986
8888+turn on 11,549 through 393,597
8989+turn off 842,893 through 976,911
9090+toggle 274,106 through 581,329
9191+toggle 406,403 through 780,950
9292+toggle 408,988 through 500,994
9393+toggle 217,73 through 826,951
9494+turn on 917,872 through 961,911
9595+toggle 394,34 through 510,572
9696+toggle 424,603 through 583,626
9797+toggle 106,159 through 755,738
9898+turn off 244,610 through 472,709
9999+turn on 350,265 through 884,690
100100+turn on 688,184 through 928,280
101101+toggle 279,443 through 720,797
102102+turn off 615,493 through 888,610
103103+toggle 118,413 through 736,632
104104+turn on 798,782 through 829,813
105105+turn off 250,934 through 442,972
106106+turn on 68,503 through 400,949
107107+toggle 297,482 through 313,871
108108+toggle 710,3 through 839,859
109109+turn on 125,300 through 546,888
110110+toggle 482,39 through 584,159
111111+turn off 536,89 through 765,962
112112+turn on 530,518 through 843,676
113113+turn on 994,467 through 994,676
114114+turn on 623,628 through 744,927
115115+toggle 704,912 through 837,983
116116+turn on 154,364 through 517,412
117117+toggle 344,409 through 780,524
118118+turn off 578,740 through 725,879
119119+turn on 251,933 through 632,957
120120+turn on 827,705 through 971,789
121121+toggle 191,282 through 470,929
122122+toggle 324,525 through 446,867
123123+toggle 534,343 through 874,971
124124+toggle 550,650 through 633,980
125125+toggle 837,404 through 881,915
126126+toggle 338,881 through 845,905
127127+turn on 469,462 through 750,696
128128+turn on 741,703 through 892,870
129129+turn off 570,215 through 733,562
130130+turn on 445,576 through 870,775
131131+turn on 466,747 through 554,878
132132+turn off 820,453 through 868,712
133133+turn off 892,706 through 938,792
134134+turn off 300,238 through 894,746
135135+turn off 306,44 through 457,444
136136+turn off 912,569 through 967,963
137137+toggle 109,756 through 297,867
138138+turn on 37,546 through 41,951
139139+turn on 321,637 through 790,910
140140+toggle 66,50 through 579,301
141141+toggle 933,221 through 933,791
142142+turn on 486,676 through 878,797
143143+turn on 417,231 through 556,317
144144+toggle 904,468 through 981,873
145145+turn on 417,675 through 749,712
146146+turn on 692,371 through 821,842
147147+toggle 324,73 through 830,543
148148+turn on 912,490 through 977,757
149149+turn off 634,872 through 902,949
150150+toggle 266,779 through 870,798
151151+turn on 772,982 through 990,996
152152+turn off 607,46 through 798,559
153153+turn on 295,602 through 963,987
154154+turn on 657,86 through 944,742
155155+turn off 334,639 through 456,821
156156+turn off 997,667 through 997,670
157157+turn off 725,832 through 951,945
158158+turn off 30,120 through 952,984
159159+turn on 860,965 through 917,976
160160+toggle 471,997 through 840,998
161161+turn off 319,307 through 928,504
162162+toggle 823,631 through 940,908
163163+toggle 969,984 through 981,993
164164+turn off 691,319 through 865,954
165165+toggle 911,926 through 938,929
166166+turn on 953,937 through 968,991
167167+toggle 914,643 through 975,840
168168+turn on 266,982 through 436,996
169169+turn off 101,896 through 321,932
170170+turn off 193,852 through 751,885
171171+turn off 576,532 through 863,684
172172+turn on 761,456 through 940,783
173173+turn on 20,290 through 398,933
174174+turn off 435,335 through 644,652
175175+turn on 830,569 through 905,770
176176+turn off 630,517 through 905,654
177177+turn on 664,53 through 886,976
178178+toggle 275,416 through 408,719
179179+turn on 370,621 through 515,793
180180+turn on 483,373 through 654,749
181181+turn on 656,786 through 847,928
182182+turn off 532,752 through 945,974
183183+toggle 301,150 through 880,792
184184+turn off 951,488 through 958,952
185185+turn on 207,729 through 882,828
186186+toggle 694,532 through 973,961
187187+toggle 676,639 through 891,802
188188+turn off 653,6 through 905,519
189189+toggle 391,109 through 418,312
190190+turn on 877,423 through 957,932
191191+turn on 340,145 through 563,522
192192+turn off 978,467 through 988,895
193193+turn off 396,418 through 420,885
194194+turn off 31,308 through 816,316
195195+turn on 107,675 through 758,824
196196+turn on 61,82 through 789,876
197197+turn on 750,743 through 754,760
198198+toggle 88,733 through 736,968
199199+turn off 754,349 through 849,897
200200+toggle 157,50 through 975,781
201201+turn off 230,231 through 865,842
202202+turn off 516,317 through 630,329
203203+turn off 697,820 through 829,903
204204+turn on 218,250 through 271,732
205205+toggle 56,167 through 404,431
206206+toggle 626,891 through 680,927
207207+toggle 370,207 through 791,514
208208+toggle 860,74 through 949,888
209209+turn on 416,527 through 616,541
210210+turn off 745,449 through 786,908
211211+turn on 485,554 through 689,689
212212+turn on 586,62 through 693,141
213213+toggle 506,759 through 768,829
214214+turn on 473,109 through 929,166
215215+turn on 760,617 through 773,789
216216+toggle 595,683 through 618,789
217217+turn off 210,775 through 825,972
218218+toggle 12,426 through 179,982
219219+turn on 774,539 through 778,786
220220+turn on 102,498 through 121,807
221221+turn off 706,897 through 834,965
222222+turn off 678,529 through 824,627
223223+turn on 7,765 through 615,870
224224+turn off 730,872 through 974,943
225225+turn off 595,626 through 836,711
226226+turn off 215,424 through 841,959
227227+toggle 341,780 through 861,813
228228+toggle 507,503 through 568,822
229229+turn on 252,603 through 349,655
230230+toggle 93,521 through 154,834
231231+turn on 565,682 through 951,954
232232+turn on 544,318 through 703,418
233233+toggle 756,953 through 891,964
234234+turn on 531,123 through 856,991
235235+turn on 148,315 through 776,559
236236+turn off 925,835 through 963,971
237237+turn on 895,944 through 967,964
238238+turn off 102,527 through 650,747
239239+toggle 626,105 through 738,720
240240+turn off 160,75 through 384,922
241241+toggle 813,724 through 903,941
242242+turn on 207,107 through 982,849
243243+toggle 750,505 through 961,697
244244+toggle 105,410 through 885,819
245245+turn on 226,104 through 298,283
246246+turn off 224,604 through 508,762
247247+turn on 477,368 through 523,506
248248+turn off 477,901 through 627,936
249249+turn off 887,131 through 889,670
250250+turn on 896,994 through 938,999
251251+toggle 401,580 through 493,728
252252+toggle 987,184 through 991,205
253253+turn on 821,643 through 882,674
254254+toggle 784,940 through 968,959
255255+turn off 251,293 through 274,632
256256+turn off 339,840 through 341,844
257257+turn off 675,351 through 675,836
258258+toggle 918,857 through 944,886
259259+toggle 70,253 through 918,736
260260+turn off 612,604 through 772,680
261261+turn off 277,40 through 828,348
262262+toggle 692,139 through 698,880
263263+toggle 124,446 through 883,453
264264+toggle 969,932 through 990,945
265265+toggle 855,692 through 993,693
266266+toggle 722,472 through 887,899
267267+toggle 978,149 through 985,442
268268+toggle 837,540 through 916,889
269269+turn off 612,2 through 835,82
270270+toggle 560,767 through 878,856
271271+turn on 461,734 through 524,991
272272+toggle 206,824 through 976,912
273273+turn on 826,610 through 879,892
274274+turn on 577,699 through 956,933
275275+turn off 9,250 through 50,529
276276+turn off 77,657 through 817,677
277277+turn on 68,419 through 86,426
278278+turn on 991,720 through 992,784
279279+turn on 668,20 through 935,470
280280+turn off 133,418 through 613,458
281281+turn off 487,286 through 540,328
282282+toggle 247,874 through 840,955
283283+toggle 301,808 through 754,970
284284+turn off 34,194 through 578,203
285285+turn off 451,49 through 492,921
286286+turn on 907,256 through 912,737
287287+turn off 479,305 through 702,587
288288+turn on 545,583 through 732,749
289289+toggle 11,16 through 725,868
290290+turn on 965,343 through 986,908
291291+turn on 674,953 through 820,965
292292+toggle 398,147 through 504,583
293293+turn off 778,194 through 898,298
294294+turn on 179,140 through 350,852
295295+turn off 241,118 through 530,832
296296+turn off 41,447 through 932,737
297297+turn off 820,663 through 832,982
298298+turn on 550,460 through 964,782
299299+turn on 31,760 through 655,892
300300+toggle 628,958 through 811,992
+120
lib/day6.js
···11+var R_PARSE_INSTRUCTION = /^(.+)\s(\d+),(\d+)\sthrough\s(\d+),(\d+)/;
22+33+module.exports = {
44+55+ processInstruction1: function(grid, instruction) {
66+ var parts = instruction.match(R_PARSE_INSTRUCTION);
77+88+ if (!parts) return grid; // Invalid instruction
99+1010+ var command = parts[1];
1111+ var from = [parseInt(parts[2]), parseInt(parts[3])];
1212+ var to = [parseInt(parts[4]), parseInt(parts[5])];
1313+1414+ switch (command) {
1515+ case 'turn on': return this.turnOn(grid, from, to);
1616+ case 'turn off': return this.turnOff(grid, from, to);
1717+ case 'toggle': return this.toggle(grid, from, to);
1818+ }
1919+ },
2020+2121+ processInstruction2: function(grid, instruction) {
2222+ var parts = instruction.match(R_PARSE_INSTRUCTION);
2323+2424+ if (!parts) return grid; // Invalid instruction
2525+2626+ var command = parts[1];
2727+ var from = [parseInt(parts[2]), parseInt(parts[3])];
2828+ var to = [parseInt(parts[4]), parseInt(parts[5])];
2929+3030+ switch (command) {
3131+ case 'turn on': return this.increaseBrightness(grid, from, to);
3232+ case 'turn off': return this.reduceBrightness(grid, from, to);
3333+ case 'toggle':
3434+ return this.increaseBrightness(
3535+ this.increaseBrightness(grid, from, to),
3636+ from, to);
3737+ }
3838+ },
3939+4040+ buildGrid: function(x, y) {
4141+ var grid = [];
4242+4343+ for (var i = 0; i < y; i++) {
4444+ grid.push([]);
4545+ }
4646+4747+ grid.forEach(function(row) {
4848+ for (var i = 0; i < x; i++) {
4949+ row.push(0);
5050+ }
5151+ });
5252+5353+ return grid;
5454+ },
5555+5656+ turnOn: function(grid, from, to) {
5757+ return setGridValues(grid, from, to, 1);
5858+ },
5959+ turnOff: function(grid, from, to) {
6060+ return setGridValues(grid, from, to, 0);
6161+ },
6262+ toggle: function(grid, from, to) {
6363+ return setGridValues(grid, from, to, 'toggle');
6464+ },
6565+ increaseBrightness: function(grid, from, to) {
6666+ return changeGridValues(grid, from, to, 1);
6767+ },
6868+ reduceBrightness: function(grid, from, to) {
6969+ return changeGridValues(grid, from, to, -1);
7070+ },
7171+7272+ countLightsOn: function(grid) {
7373+ var count = 0;
7474+7575+ grid.forEach(function(row) {
7676+ row.forEach(function(light) {
7777+ if (light) count++;
7878+ });
7979+ });
8080+8181+ return count;
8282+ },
8383+8484+ totalBrightness: function(grid) {
8585+ var total = 0;
8686+8787+ grid.forEach(function(row) {
8888+ row.forEach(function(light) {
8989+ total += light;
9090+ });
9191+ });
9292+9393+ return total;
9494+ }
9595+};
9696+9797+function setGridValues(grid, from, to, value) {
9898+ var newGrid = grid;
9999+ for (var i = from[0]; i <= to[0]; i++) {
100100+ for (var j = from[1]; j <= to[1]; j++) {
101101+ if (value === 'toggle') {
102102+ newGrid[i][j] = grid[i][j] === 0 ? 1 : 0;
103103+ } else {
104104+ newGrid[i][j] = value;
105105+ }
106106+ }
107107+ }
108108+ return grid;
109109+}
110110+111111+function changeGridValues(grid, from, to, value) {
112112+ var newGrid = grid;
113113+ for (var i = from[0]; i <= to[0]; i++) {
114114+ for (var j = from[1]; j <= to[1]; j++) {
115115+ newGrid[i][j] = grid[i][j] + (value);
116116+ if (newGrid[i][j] < 0) newGrid[i][j] = 0;
117117+ }
118118+ }
119119+ return grid;
120120+}
+146
spec/day6.spec.js
···11+var expect = require('chai').expect;
22+var Day6 = require('../lib/day6');
33+44+describe('Day6', function() {
55+ var grid;
66+77+ beforeEach(function() {
88+ grid = Day6.buildGrid(2, 3);
99+ });
1010+1111+ describe('#buildGrid', function() {
1212+ it('builds a grid of booleans from specified dimensions', function() {
1313+ expect(grid).to.eql([
1414+ [0, 0],
1515+ [0, 0],
1616+ [0, 0]
1717+ ]);
1818+ });
1919+ });
2020+2121+ describe('#turnOn', function() {
2222+ it('makes sure lights are ON in specified range', function() {
2323+ grid = Day6.turnOn(grid, [0,0], [1,1]);
2424+ expect(grid).to.eql([
2525+ [1, 1],
2626+ [1, 1],
2727+ [0, 0]
2828+ ]);
2929+ });
3030+ });
3131+3232+ describe('#turnOff', function() {
3333+ it('makes sure lights are OFF in specified range', function() {
3434+ grid = Day6.turnOn(grid, [0,0], [1,1]);
3535+ grid = Day6.turnOff(grid, [0,1], [1,1]);
3636+3737+ expect(grid).to.eql([
3838+ [1, 0],
3939+ [1, 0],
4040+ [0, 0]
4141+ ]);
4242+ });
4343+ });
4444+4545+ describe('#toggle', function() {
4646+ it('TOGGLES the lights at specified range', function() {
4747+ grid = Day6.turnOn(grid, [0,0], [1,1]);
4848+ grid = Day6.toggle(grid, [0,1], [1,1]);
4949+5050+ expect(grid).to.eql([
5151+ [1, 0],
5252+ [1, 0],
5353+ [0, 0]
5454+ ]);
5555+ });
5656+ });
5757+5858+ describe('#reduceBrightness', function() {
5959+ it('reduces the brightness of specified range by 1, to a min of 0', function() {
6060+ grid = Day6.toggle(grid, [0,0], [2,1]);
6161+ grid = Day6.reduceBrightness(grid, [0,0], [0,1]);
6262+ grid = Day6.reduceBrightness(grid, [0,0], [0,1]);
6363+6464+ expect(grid).to.eql([
6565+ [0, 0],
6666+ [1, 1],
6767+ [1, 1]
6868+ ]);
6969+ });
7070+ });
7171+7272+ describe('#increaseBrightness', function() {
7373+ it('increase the brightness of a specified range by 1', function() {
7474+ grid = Day6.toggle(grid, [0,0], [2,1]);
7575+ grid = Day6.increaseBrightness(grid, [0,0], [0,1]);
7676+7777+ expect(grid).to.eql([
7878+ [2, 2],
7979+ [1, 1],
8080+ [1, 1]
8181+ ]);
8282+ });
8383+ });
8484+8585+ describe('#totalBrightness', function() {
8686+ it('calculates the total brightness of all lights in the grid', function() {
8787+ grid = Day6.toggle(grid, [0,0], [2,1]);
8888+8989+ expect(grid).to.eql([
9090+ [1, 1],
9191+ [1, 1],
9292+ [1, 1]
9393+ ]);
9494+9595+ expect(Day6.totalBrightness(grid)).to.equal(6);
9696+ });
9797+ });
9898+9999+ describe('#processInstruction1', function() {
100100+ it('turns a text instruction into a command with to and from coords', function() {
101101+ var instructions = [
102102+ 'turn on 0,0 through 1,1',
103103+ 'turn off 0,1 through 1,1',
104104+ 'toggle 0,0 through 2,0'
105105+ ];
106106+107107+ instructions.forEach(function(instruction) {
108108+ grid = Day6.processInstruction1(grid, instruction);
109109+ });
110110+111111+ expect(grid).to.eql([
112112+ [0, 0],
113113+ [0, 0],
114114+ [1, 0]
115115+ ]);
116116+ });
117117+ });
118118+119119+ describe('#processInstruction2', function() {
120120+ it('turns a text instruction into a command with to and from coords', function() {
121121+ var instructions = [
122122+ 'turn off 0,0 through 2,1',
123123+ 'turn on 0,0 through 1,1',
124124+ 'turn off 0,1 through 1,1',
125125+ 'toggle 0,0 through 2,0'
126126+ ];
127127+128128+ instructions.forEach(function(instruction) {
129129+ grid = Day6.processInstruction2(grid, instruction);
130130+ });
131131+132132+ expect(grid).to.eql([
133133+ [3, 0],
134134+ [3, 0],
135135+ [2, 0]
136136+ ]);
137137+ });
138138+ });
139139+140140+ describe('#countLightsOn', function() {
141141+ it('returns the number of lights that are on', function() {
142142+ grid = Day6.turnOn(grid, [0,0], [1,0]);
143143+ expect(Day6.countLightsOn(grid)).to.equal(2);
144144+ });
145145+ });
146146+});