-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.js
96 lines (89 loc) · 2.52 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
87
88
89
90
91
92
93
94
95
96
import fs from "fs";
import assert from "assert";
import * as R from "ramda";
const parseInput = (input) => input.split("\n\n").map((block) => block.split("\n").map(JSON.parse));
function smaller(left, right) {
if (typeof left === "number" && typeof right === "number")
return left === right ? 0 : left < right ? 1 : -1;
else if (left instanceof Array && right instanceof Array) {
const shorter = left.length < right.length ? left : right;
const foo = shorter.map((_, index) => smaller(left[index], right[index]));
if (foo.every((num) => num === 0)) {
return left.length === right.length
? 0
: left.length === shorter.length
? 1
: -1;
}
else {
return foo.find((num) => num === 1 || num === -1);
}
}
else if (left instanceof Array) {
return smaller(left, [right]);
}
else {
return smaller([left], right);
}
}
const arrange = (packets) => {
return packets.sort(smaller).reverse();
};
const part1 = (input) => R.sum(parseInput(input).map(([left, right], index) => {
return smaller(left, right) === 1 ? index + 1 : 0;
}));
const part2 = (input) => {
const arranged = arrange(parseInput(input)
.flat()
.concat([[2]])
.concat([[6]]));
return ((arranged.findIndex((elem) => elem.length === 1 && elem[0] === 2) + 1) *
(arranged.findIndex((elem) => elem.length === 1 && elem[0] === 6) + 1));
};
const testInput = `[1,1,3,1,1]
[1,1,5,1,1]
[[1],[2,3,4]]
[[1],4]
[9]
[[8,7,6]]
[[4,4],4,4]
[[4,4],4,4,4]
[7,7,7,7]
[7,7,7]
[]
[3]
[[[]]]
[[]]
[1,[2,[3,[4,[5,6,7]]]],8,9]
[1,[2,[3,[4,[5,6,0]]]],8,9]`;
const testOutput = `[]
[[]]
[[[]]]
[1,1,3,1,1]
[1,1,5,1,1]
[[1],[2,3,4]]
[1,[2,[3,[4,[5,6,0]]]],8,9]
[1,[2,[3,[4,[5,6,7]]]],8,9]
[[1],4]
[[2]]
[3]
[[4,4],4,4]
[[4,4],4,4,4]
[[6]]
[7,7,7]
[7,7,7,7]
[[8,7,6]]
[9]`;
assert(smaller([1, 1, 3, 1, 1], [1, 1, 5, 1, 1]) === 1);
assert(smaller([[1], [2, 3, 4]], [[1], 4]) === 1);
assert(smaller([9], [[8, 7, 6]]) === -1);
assert(smaller([[4, 4], 4, 4], [[4, 4], 4, 4, 4]) === 1);
assert(smaller([7, 7, 7, 7], [7, 7, 7]) === -1);
assert(smaller([], [3]) === 1);
assert(smaller([[[]]], [[]]) === -1);
assert(smaller([1, [2, [3, [4, [5, 6, 7]]]], 8, 9], [1, [2, [3, [4, [5, 6, 0]]]], 8, 9]) === -1);
assert(part1(testInput) === 13);
assert(part2(testInput) === 140);
const input = fs.readFileSync("inputs/day13.txt", "utf-8");
console.log(`Part 1: ${part1(input)}`);
console.log(`Part 2: ${part2(input)}`);