-
Notifications
You must be signed in to change notification settings - Fork 0
/
2022-13.js
46 lines (35 loc) · 1.05 KB
/
2022-13.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
const fs = require("fs")
const readData = () => {
const data = fs
.readFileSync("./input2022-13.txt", "utf-8")
.split(/\r?\n\r?\n/)
.map(line => line.split(/\r?\n/).map(part => JSON.parse(part)))
return data
}
const main = () => {
const pairs = readData()
const compare = ([left, right]) => {
if ([left, right].every(Number.isInteger)) {
if (left < right) return true
if (left > right) return false
return
}
if ([left, right].every(Array.isArray)) {
for (let i = 0; i < Math.min(left.length, right.length); i++) {
const res = compare([left[i], right[i]])
if (res != null) return res
}
return compare([left.length, right.length])
}
return compare([[left].flat(), [right].flat()])
}
const dividers = [[[2]], [[6]]]
const res = [...pairs.flat(), ...dividers]
.sort((left, right) => compare([right, left]) - compare([left, right]))
.reduce(
(acc, el, index) => (dividers.includes(el) ? acc * (index + 1) : acc),
1
)
console.log(res)
}
main()