-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfuns.js
2360 lines (2335 loc) · 75.7 KB
/
funs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function () {
'use strict';
const all = {
day1: {
part1: (data) => {
const left = /^\D*?(\d)/;
const right = /(\d)\D*?$/;
const elves = data.trim().split('\n').map(cals => {
return cals.match(left)[1] + '' + cals.match(right)[1];
}).map(Number);
console.log(elves);
return elves.reduce((acc, item) => {
return acc + item;
}, 0);
},
part2: (data) => {
const matchD = /one|two|three|four|five|six|seven|eight|nine|\d/;
const matchD2 = /enin|thgie|neves|xis|evif|ruof|eerht|owt|eno|\d/;
const digitVal = { one: 1, two: 2, three: 3, four: 4, five: 5, six: 6, seven: 7, eight: 8, nine: 9 };
for (let i = 1; i <= 9; i++) {
digitVal['' + i] = i;
}
const elves = data.trim().split('\n').map(cals => {
const slac = cals.split('').reverse().join('');
const num = cals.match(matchD)[0].replace(matchD, m => digitVal[m]) + '' + slac.match(matchD2)[0].split('').reverse().join('').replace(matchD, m => digitVal[m]);
return num;
});
console.log(elves);
// 54558 is too low
// 54490 is too low
const elves2 = elves.map(Number);
return elves2.reduce((acc, item) => {
return acc + item;
}, 0);
}
},
day2: {
part1: (data) => {
// Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
const gn = /^Game (\d+): /;
const gs = /(\d+)\s([rgb])/;
let possible = 0;
data.trim().split('\n').forEach(g => {
const gameNum = +g.match(gn)[1];
const right = g.split(':')[1].trim();
const game = {
gameNum,
r: 0,
g: 0,
b: 0,
d: 0,
dd: 0,
p: 0
};
right.split(';').forEach(s => {
game.d++;
s.split(',').forEach(c => {
const cube = c.match(gs);
game.dd++;
game[cube[2]] = Math.max(+cube[1], game[cube[2]]);
});
});
// 12 red cubes, 13 green cubes, and 14 blue cubes
if (game.r <= 12 && game.g <= 13 && game.b <= 14) {
possible += gameNum;
game.p = 1;
}
console.log(game);
});
// not 234
return possible;
},
part2: (data) => {
const gn = /^Game (\d+): /;
const gs = /(\d+)\s([rgb])/;
let sum = 0;
data.trim().split('\n').forEach(g => {
const gameNum = +g.match(gn)[1];
const right = g.split(':')[1].trim();
const game = {
gameNum,
r: 0,
g: 0,
b: 0,
s: 0
};
right.split(';').forEach(s => {
s.split(',').forEach(c => {
const cube = c.match(gs);
game[cube[2]] = Math.max(+cube[1], game[cube[2]]);
});
});
game.s = game.r * game.g * game.b;
sum += game.s;
console.log(game);
});
return sum;
}
},
day3: {
part1: (data) => {
const nums = /(\d+)/g;
const sym = /[^0-9.]/;
const parts = [];
const elves = data.trim().split('\n').map(cals => {
return {
matches: [...cals.matchAll(nums)],
row: cals.split(''),
len: cals.length
};
});
for (let y = 0; y < elves.length; y++) {
console.log(elves[y].matches);
for (const m of elves[y].matches) {
const low = Math.max(0, m.index - 1);
const val = m[1];
const hi = Math.min(elves[y].len - 1, m.index + m[0].length);
console.log(val, low, hi);
let isClean = true;
let char = '';
if (y > 0) { // look prev if not first
for (let x = low; x <= hi; x++) {
if (sym.test(elves[y - 1].row[x])) {
char = elves[y - 1].row[x];
isClean = false;
break;
}
}
}
// need to test left & right
if (isClean && sym.test(elves[y].row[low])) {
char = elves[y].row[low];
isClean = false;
}
if (isClean && sym.test(elves[y].row[hi])) {
char = elves[y].row[hi];
isClean = false;
}
if (isClean && y < (elves.length - 1)) {
// look next
for (let x = low; x <= hi; x++) {
if (sym.test(elves[y + 1].row[x])) {
char = elves[y + 1].row[x];
isClean = false;
break;
}
}
}
if (!isClean) {
parts.push({
val: +val,
ch: char
});
}
}
}
console.log('parts', parts);
// 533578 is too high
// 530940 is too high
return parts.reduce((acc, item) => {
return acc + item.val;
}, 0);
},
part2: (data) => {
const nums = /(\d+)/g;
const sym = /[^0-9.]/;
const gears = {};
const ratios = [];
const elves = data.trim().split('\n').map(cals => {
return {
matches: [...cals.matchAll(nums)],
row: cals.split(''),
len: cals.length
};
});
for (let y = 0; y < elves.length; y++) {
for (const m of elves[y].matches) {
const low = Math.max(0, m.index - 1);
const val = m[1];
const hi = Math.min(elves[y].len - 1, m.index + m[0].length);
let isClean = true;
let char = '';
let charx = -1;
let chary = -1;
if (y > 0) { // look prev if not first
for (let x = low; x <= hi; x++) {
if (sym.test(elves[y - 1].row[x])) {
char = elves[y - 1].row[x];
chary = y - 1;
charx = x;
isClean = false;
break;
}
}
}
// need to test left & right
if (isClean && sym.test(elves[y].row[low])) {
char = elves[y].row[low];
chary = y;
charx = low;
isClean = false;
}
if (isClean && sym.test(elves[y].row[hi])) {
char = elves[y].row[hi];
chary = y;
charx = hi;
isClean = false;
}
if (isClean && y < (elves.length - 1)) {
// look next
for (let x = low; x <= hi; x++) {
if (sym.test(elves[y + 1].row[x])) {
char = elves[y + 1].row[x];
chary = y + 1;
charx = x;
isClean = false;
break;
}
}
}
if (!isClean && char === '*') {
const gearid = charx + ',' + chary;
if (!gears[gearid]) {
gears[gearid] = [];
}
gears[gearid].push(+val);
if (gears[gearid].length === 2) {
ratios.push(gears[gearid][0] * gears[gearid][1]);
}
}
}
}
console.log('gears', gears);
return ratios.reduce((acc, item) => {
return acc + item;
}, 0);
}
},
day4: {
part1: (data) => {
const lotto = data.trim().split('\n').map(cals => {
const line = cals.split('|');
const card = line[0].split(':');
const values = card[1].trim().split(/\s+/).map(Number);
const winner = line[1].trim().split(/\s+/).map(Number);
const good = values.filter(n => winner.includes(n));
let score = good.length;
if (score > 0) {
score = 2 ** (score - 1);
}
return score;
});
return lotto.reduce((acc, item) => acc + item, 0);
},
part2: (data) => {
const copies = {};
const lotto = data.trim().split('\n').map(cals => {
const line = cals.split('|');
const card = line[0].split(':');
const num = +card[0].match(/\d+/)[0];
const values = card[1].trim().split(/\s+/).map(Number);
const winner = line[1].trim().split(/\s+/).map(Number);
const good = values.filter(n => winner.includes(n));
const score = good.length;
if (score > 0) {
const inc = 1 + (copies[num] || 0);
for (let i = num + 1; i <= num + score; i++) {
if (!copies[i]) {
copies[i] = 0;
}
copies[i] += inc;
}
}
return score;
});
console.log(lotto, copies);
return lotto.length + Object.values(copies).reduce((acc, item) => acc + item, 0);
}
},
day5: {
part1: (data) => {
const almanac = data.trim().split('\n\n');
const seeds = almanac[0].split(':')[1].trim().split(' ').map(Number);
const maps = almanac.slice(1).map(m => {
const lines = m.split('\n');
const matchName = /(\w+)-to-(\w+)/;
const matched = lines[0].match(matchName);
const source = matched[1];
const dest = matched[2];
const routes = lines.slice(1).map(l => {
const nums = l.split(' ').map(Number);
return {
s1: nums[1],
s2: nums[1] + nums[2],
d1: nums[0],
d2: nums[0] + nums[2],
r: nums[2]
};
});
return {
source,
dest,
routes
};
});
console.log(seeds, maps);
const paths = seeds.reduce((acc, s) => {
acc.push({ seed: s });
return acc;
}, []);
let step = 'seed';
maps.forEach(m => {
const next = m.dest;
paths.forEach(p => {
const val = p[step];
let isMapped = false;
for (let i = 0; i < m.routes.length; i++) {
if (val >= m.routes[i].s1 && val <= m.routes[i].s2) {
p[next] = (val - m.routes[i].s1) + m.routes[i].d1;
isMapped = true;
break;
}
}
if (!isMapped) {
p[next] = val;
}
});
step = next;
});
console.log(paths);
return Math.min(...paths.map(p => p.location));
},
part2: (data) => {
const almanac = data.trim().split('\n\n');
const seedRanges = almanac[0].split(':')[1].trim().split(' ').map(Number);
const seedPairs = {};
for (let i = 0; i < seedRanges.length; i += 2) {
seedPairs[seedRanges[i]] = seedRanges[i + 1];
}
const maps = almanac.slice(1).map(m => {
const lines = m.split('\n');
const matchName = /(\w+)-to-(\w+)/;
const matched = lines[0].match(matchName);
const sourceName = matched[1];
const destName = matched[2];
const routes = lines.slice(1).map(l => {
const nums = l.split(' ').map(Number);
return {
sourceStart: nums[1],
sourceEnd: nums[1] + nums[2] - 1,
dest: nums[0]
};
});
return {
sourceName,
destName,
routes
};
});
const routes = maps.map(m => m.routes);
console.log(Object.keys(seedPairs).length, routes);
let smallest = Infinity;
let seedGroup = 0;
Object.keys(seedPairs).forEach(seedKey => {
const seedStart = +seedKey;
const seedRange = seedPairs[seedKey];
const seedEnd = seedStart + seedRange;
console.log(++seedGroup, seedStart, seedRange, seedEnd);
for (let seed = seedStart; seed < seedEnd; seed++) {
const last = routes.reduce((seedPos, routeList) => {
const newRoute = routeList.find(route => seedPos >= route.sourceStart && seedPos <= route.sourceEnd);
const newPos = newRoute ? (seedPos - newRoute.sourceStart) + newRoute.dest : seedPos;
return newPos;
}, seed);
smallest = Math.min(smallest, last);
}
});
console.log(smallest);
return smallest;
}
},
day6: {
part1: (data) => {
const input = data.trim().split('\n').map(l => l.split(':')[1].trim().split(/\s+/).map(Number));
const times = input[0];
const dists = input[1];
console.log(times, dists);
const actual = {};
for (let t = 0; t < times.length; t++) {
const time = times[t];
const dist = dists[t];
actual[t] = [];
for (let b = 0; b <= time; b++) {
const race = b * (time - b);
if (race > dist) {
actual[t].push(race);
}
}
}
console.log(actual);
return Object.values(actual).reduce((acc, a) => acc * a.length, 1);
},
part2: (data) => {
const input = data.trim().split('\n').map(l => l.split(':')[1].trim().split(/\s+/).join('')).map(Number);
const time = input[0];
const dist = input[1];
console.log(time, dist);
let wins = 0;
for (let b = 0; b <= time; b++) {
const race = b * (time - b);
if (race > dist) {
wins++;
}
}
console.log(wins);
return wins;
}
},
day7: {
part1: (data) => {
const cards = '23456789TJQKA'.split('');
const cardVal = c => {
return cards.indexOf(c) + 1;
};
const score2 = h => {
const val = {
five: 0,
four: 0,
full: 0,
three: 0,
twopair: 0,
pair: 0,
high: 0
};
if (h.every(val => val === h[0])) {
val.five = 1;
} else {
cards.slice().reverse().forEach(c1 => {
const count = h.filter(c2 => c1 === c2).length;
if (count === 4) {
val.four = 1;
} else if (count === 3) {
val.three = 1;
} else if (count === 2) {
val.pair += 1;
} else if (count === 1) {
val.high = 1;
}
});
if (val.three === 1 && val.pair === 1) {
val.full = 1;
val.three = 0;
val.pair = 0;
} else if (val.pair === 2) {
val.twopair = 1;
val.pair = 0;
}
}
return Object.values(val);
};
const input = data.trim().split('\n').map(l => {
const line = l.split(' ');
const o = {
raw: line[0],
hand: line[0].split(''),
bid: +line[1]
};
o.score = score2(o.hand);
o.vals = o.hand.reduce((a, v) => {
a.push(cardVal(v));
return a;
}, []);
return o;
});
const sorted = input.slice().sort((a, b) => {
for (let i = 0; i < 7; i++) {
const as = a.score[i];
const bs = b.score[i];
if (as < bs) {
return -1;
} else if (as > bs) {
return 1;
} else if (as > 0 && bs > 0) {
for (let j = 0; j < 5; j++) {
const ac = a.vals[j];
const bc = b.vals[j];
if (ac < bc) {
return -1;
} else if (ac > bc) {
return 1;
}
}
}
}
return 0;
});
const answer = sorted.reduce((a, v, i) => a + (v.bid * (i + 1)), 0);
console.log(input, sorted, answer);
// 250946742
// 251308771 too high
return answer;
},
part2: (data) => {
const cards = 'J23456789TQKA'.split('');
const cardVal = c => cards.indexOf(c) + 1;
const score2 = h => {
const val = {
five: 0,
four: 0,
full: 0,
three: 0,
twopair: 0,
pair: 0,
high: 0
};
const cs = cards.slice(1);
const csl = cs.length;
for (let i = 5; i > 0; i--) {
for (let j = 0; j < csl; j++) {
const c1 = cs[j];
const matched = h.filter(c2 => c1 === c2 || c2 === 'J');
const count = matched.length;
if (count === i) {
if (count === 5) {
val.five = 1;
h = h.join('').replace(/J/g, c1).split('');
i = 0;
j = csl;
} else if (count === 4) {
val.four = 1;
val.high = 1;
h = h.join('').replace(/J/g, c1).split('');
i = 0;
j = csl;
} else if (count === 3) {
val.three = 1;
h = h.join('').replace(/J/g, c1).split('');
} else if (count === 2) {
val.pair += 1;
h = h.join('').replace(/J/g, c1).split('');
} else if (count === 1) {
val.high = 1;
}
}
}
}
if (val.three === 1 && val.pair === 1) {
val.full = 1;
val.three = 0;
val.pair = 0;
} else if (val.pair === 2) {
val.twopair = 1;
val.pair = 0;
}
return Object.values(val);
};
const input = data.trim().split('\n').map(l => {
const line = l.split(' ');
const o = {
raw: line[0],
hand: line[0].split(''),
bid: +line[1]
};
o.score = score2(o.hand);
o.vals = o.hand.reduce((a, v) => {
a.push(cardVal(v));
return a;
}, []);
return o;
});
const sorted = input.slice().sort((a, b) => {
for (let i = 0; i < 7; i++) {
const as = a.score[i];
const bs = b.score[i];
if (as < bs) {
return -1;
} else if (as > bs) {
return 1;
} else if (as > 0 && bs > 0) {
for (let j = 0; j < 5; j++) {
const ac = a.vals[j];
const bc = b.vals[j];
if (ac < bc) {
return -1;
} else if (ac > bc) {
return 1;
}
}
}
}
return 0;
});
const answer = sorted.reduce((a, v, i) => a + (v.bid * (i + 1)), 0);
console.log(sorted, answer);
// 251824095?
// 251910588 is wrong
// 251794152 is wrong
// 251681150 is too low
// 250726533 is too low
// 251982524 is too high
return answer;
}
},
day8: {
part1: (data) => {
const input = data.trim().split('\n\n');
const directions = input[0].trim().split('');
const map = input[1].trim().split('\n').reduce((m, line) => {
m[line.substring(0, 3)] = {
L: line.substring(7, 10),
R: line.substring(12, 15)
};
return m;
}, {});
console.log(directions, map);
const len = directions.length;
let node = 'AAA';
let i = 0;
let c = 0;
let safety = 100000;
while (node !== 'ZZZ' && safety-- > 0) {
i = i % len;
const d = directions[i];
node = map[node][d];
// console.log(c, i, d, node);
i++;
c++;
}
console.log(safety, c);
return c;
},
part2: (data) => {
const gcd = (a, b) => !b ? a : gcd(b, a % b);
const lcm = (a, b) => (a * b) / gcd(a, b);
const input = data.trim().split('\n\n');
const directions = input[0].trim().split('');
const map = input[1].trim().split('\n').reduce((m, line) => {
m[line.substring(0, 3)] = {
L: line.substring(7, 10),
R: line.substring(12, 15)
};
return m;
}, {});
const nodes = Object.keys(map).filter(m => m[2] === 'A');
const zzz = n => n[2] === 'Z';
const dlen = directions.length;
const nlen = nodes.length;
const starts = nodes.slice();
console.log(nodes, directions, map, starts);
let safety = 100000;
let i = 0;
let c = 1;
const found = [];
const lowest = [];
while (lowest.length < nlen && safety-- > 0) {
i = i % dlen;
const d = directions[i];
for (let l = nlen; l--;) {
if (!found.includes(l)) {
let node = nodes[l];
node = map[node][d];
nodes[l] = node;
if (zzz(node)) {
found.push(l);
lowest.push(c);
}
}
}
// console.log(c, i, d, node);
i++;
c++;
}
const multiple = lowest.reduce((acc, l) => lcm(acc, l), 1);
console.log(safety, c, lowest, multiple);
return multiple;
}
},
day9: {
part1: (data) => {
const input = data.trim().split('\n').map(l => l.split(' ').map(Number));
const allEq = (val, _i, arr) => val === arr[0];
console.log(input);
const extra = [];
input.forEach(row => {
const agg = [row.slice()];
let cycle = 0;
let safety = 100;
do {
agg[cycle + 1] = [];
for (let l = agg[cycle].length - 1; l > 0; l--) {
agg[cycle + 1].unshift(agg[cycle][l] - agg[cycle][l - 1]);
}
cycle++;
} while (!agg[cycle].every(allEq) && safety--);
if (safety <= 0) {
console.warn('safety.');
}
// console.log(agg);
const sum = agg.reduce((acc, set) => acc + set[set.length - 1], 0);
extra.push(sum);
});
console.log(extra);
return extra.reduce((sum, val) => sum + val, 0);
},
part2: (data) => {
const input = data.trim().split('\n').map(l => l.split(' ').map(Number));
const allEq = (val, _i, arr) => val === arr[0];
console.log(input);
const extra = [];
input.forEach(row => {
const agg = [row.slice()];
let cycle = 0;
let safety = 100;
do {
agg[cycle + 1] = [];
for (let l = agg[cycle].length - 1; l > 0; l--) {
agg[cycle + 1].unshift(agg[cycle][l] - agg[cycle][l - 1]);
}
cycle++;
} while (!agg[cycle].every(allEq) && safety--);
if (safety <= 0) {
console.warn('safety.');
}
// console.log(agg);
const sub = agg.reverse().reduce((acc, set) => -acc + set[0], 0);
extra.push(sub);
});
console.log(extra);
return extra.reduce((sum, val) => sum + val, 0);
}
},
day10: {
part1: (data) => {
const map = {
S: ['N', 'E', 'S', 'W'],
'|': ['N', 'S'],
'-': ['E', 'W'],
L: ['N', 'E'],
J: ['N', 'W'],
7: ['S', 'W'],
F: ['E', 'S'],
'.': []
};
const fromDir = {
N: 'S',
E: 'W',
S: 'N',
W: 'E'
};
let startY = -1;
let startX = -1;
const input = data.trim().split('\n').map((l, y) => {
const s = l.indexOf('S');
if (s > -1) {
startY = y;
startX = s;
}
return l.split('');
});
const route = {};
const maxY = input.length;
const maxX = input[0].length;
const maxLoop = maxY * maxX;
// use a loop instead
const go = (dir, y, x, len) => {
let i = 0;
while (i++ < maxLoop) {
if (dir === 'N') {
y--;
} else if (dir === 'E') {
x++;
} else if (dir === 'S') {
y++;
} else if (dir === 'W') {
x--;
}
// console.log(dir, 'y', y, 'x', x);
if (y < 0 || y >= maxY || x < 0 || x >= maxX) {
return;
}
const char = input[y][x];
// console.log('char', char);
if (char === '.' || char === 'S') {
return;
}
const pipe = map[char];
const from = fromDir[dir];
// console.log('pipe', pipe);
if (!pipe.includes(from)) {
// bad pipe
return;
}
const next = pipe.find(c => c !== from);
len++;
const val = route[y + ':' + x] || Infinity;
route[y + ':' + x] = Math.min(val, len);
// Uncaught InternalError: too much recursion
// return go(next, y, x, len);
dir = next;
}
};
// start
route[startY + ':' + startX] = 0;
console.log('look N', startY, startX);
go('N', startY, startX, 0);
console.log('look E');
go('E', startY, startX, 0);
console.log('look S');
go('S', startY, startX, 0);
console.log('look W');
go('W', startY, startX, 0);
console.log(route);
return Math.max(...Object.values(route));
},
part2: (data) => {
const map = {
S: ['N', 'E', 'S', 'W'],
'|': ['N', 'S'],
'-': ['E', 'W'],
L: ['N', 'E'],
J: ['N', 'W'],
7: ['S', 'W'],
F: ['E', 'S'],
'.': []
};
const fromDir = {
N: 'S',
E: 'W',
S: 'N',
W: 'E'
};
let startY = -1;
let startX = -1;
const dirts = [];
const input = data.trim().split('\n').map((l, y) => {
const s = l.indexOf('S');
if (s > -1) {
startY = y;
startX = s;
}
return l.split('');
});
const shape = [];
const maxY = input.length;
const maxX = input[0].length;
const maxLoop = maxY * maxX;
// use a loop instead
const go = (dir, y, x) => {
let i = 0;
while (i++ < maxLoop) {
if (dir === 'N') {
y--;
} else if (dir === 'E') {
x++;
} else if (dir === 'S') {
y++;
} else if (dir === 'W') {
x--;
}
if (y < 0 || y >= maxY || x < 0 || x >= maxX) {
return;
}
const char = input[y][x];
if (char === '.' || char === 'S') {
return;
}
const pipe = map[char];
const from = fromDir[dir];
if (!pipe.includes(from)) {
// bad pipe
return;
}
const next = pipe.find(c => c !== from);
if (!shape.find(s => s[0] === y && s[1] === x)) {
shape.push([y, x]);
}
dir = next;
}
};
// start
shape.push([startY, startX]);
go('N', startY, startX);
go('E', startY, startX);
go('S', startY, startX);
go('W', startY, startX);
const inner = [];
const pointInPolygon = function (polygon, point) {
// A point is in a polygon if a line from the point to infinity crosses the polygon an odd number of times
let odd = false;
// For each edge (In this case for each point of the polygon and the previous one)
for (let i = 0, j = polygon.length - 1; i < polygon.length; i++) {
// If a line from the point into infinity crosses this edge
if (((polygon[i][1] > point[1]) !== (polygon[j][1] > point[1])) && // One point needs to be above, one below our y coordinate
// ...and the edge doesn't cross our Y corrdinate before our x coordinate (but between our x coordinate and infinity)
(point[0] < ((polygon[j][0] - polygon[i][0]) * (point[1] - polygon[i][1]) / (polygon[j][1] - polygon[i][1]) + polygon[i][0]))) {
// Invert odd
odd = !odd;
}
j = i;
}
// If the number of crossings was odd, the point is in the polygon
return odd;
};
for (let y = maxY - 1; y--;) {
for (let x = maxX - 1; x--;) {
if (!shape.some(s => s[0] === y && s[1] === x)) {
dirts.push([y, x]);
if (pointInPolygon(shape, [y, x])) {
inner.push([y, x]);
}
}
}
}
console.log(dirts, shape, inner);
return inner.length;
}
},
day11: {
part1: (data) => {
const dotRows = [];
const dotCols = [];
const allEmpty = c => c === '.';
const input = data.trim().split('\n').map((l, i) => {
const row = l.split('');
if (row.every(allEmpty)) {
dotRows.push(i);
}
return row;
});
for (let l = input[0].length; l--;) {
if (input.map(r => r[l]).every(allEmpty)) {
dotCols.push(l);
}
}
console.log(dotRows, dotCols);
const expanded = [];
for (let l = input.length; l--;) {
const newRow = [];
for (let ll = input[l].length; ll--;) {
newRow.unshift(input[l][ll]);
if (dotCols.includes(ll)) {
newRow.unshift('.');
}
}
expanded.unshift(newRow);
if (dotRows.includes(l)) {
expanded.unshift(newRow.slice());
}
}
console.log(expanded);
const manhattan = (y1, x1, y2, x2) => Math.abs(x2 - x1) + Math.abs(y2 - y1);
const points = expanded.reduce((acc, row, y) => {
row.forEach((c, x) => {
if (c === '#') {
acc.push([y, x]);
}
});
return acc;
}, []);
console.log(points);
const pairs = [];
points.forEach((p1, i1) => {
points.forEach((p2, i2) => {
if (i1 !== i2 && !pairs.some(pp => pp.keys.includes(i1) && pp.keys.includes(i2))) {
pairs.push({
keys: [i1, i2],
distance: manhattan(p1[0], p1[1], p2[0], p2[1])
});
}
});
});
console.log(pairs);
const sum = pairs.reduce((acc, pp) => acc + pp.distance, 0);
console.log(sum);
return sum;
},
part2: (data) => {
const dotRows = [];
const dotCols = [];
const allEmpty = c => c === '.';
const input = data.trim().split('\n').map((l, i) => {
const row = l.split('');
if (row.every(allEmpty)) {
dotRows.push(i);
}
return row;
});
for (let l = input[0].length; l--;) {
if (input.map(r => r[l]).every(allEmpty)) {
dotCols.push(l);
}