-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13.js
86 lines (71 loc) · 1.47 KB
/
day13.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
let fs = require("fs");
let log = console.log;
let inputFile = "./inputs/day13.txt";
let isN = (a) => typeof a === "number";
let isArr = Array.isArray;
let cmp = (l, r) => {
if (isN(l) && isN(r)) {
if (l === r) return null;
return l < r;
}
// one is not a number so transform both to arr
if (isN(l)) {
l = [l];
}
if (isN(r)) {
r = [r];
}
let i;
for (i = 0; i < Math.min(r.length, l.length); i++) {
let res = cmp(l[i], r[i]);
if (res === null) {
continue;
}
return res;
}
if (i === r.length && r.length < l.length) return false;
if (r.length === l.length) return null;
return true;
};
function part1() {
let input = fs
.readFileSync(inputFile)
.toString()
.trim()
.split("\n\n")
.map((pairs) => pairs.split("\n").map((l) => JSON.parse(l)));
let sum = 0;
for (let i = 0; i < input.length; i++) {
let [left, right] = input[i];
if (cmp(left, right)) {
sum += i + 1;
}
}
log(sum);
}
function part2() {
let input = fs
.readFileSync(inputFile)
.toString()
.trim()
.split("\n")
.filter((s) => s)
.map((s) => JSON.parse(s));
input.push([[2]]);
input.push([[6]]);
input.sort((a, b) => {
if (cmp(a, b)) return -1;
else return 1;
});
let key = 1;
input.forEach((a, i) => {
if (
JSON.stringify([[2]]) === JSON.stringify(a) ||
JSON.stringify([[6]]) === JSON.stringify(a)
) {
key *= i +1;
}
});
log(key);
}
part2();