advent-of-code-2022/day13/index.js

48 lines
1.7 KiB
JavaScript
Raw Normal View History

2022-12-13 21:06:16 +00:00
import { readFileSync } from "fs";
const input = readFileSync("packets.txt", "utf-8");
function compareArrays(left, right) {
let index = 0;
while(true) {
if(typeof left[index] === "undefined" && typeof right[index] === "undefined") return 0;
else if(typeof left[index] === "undefined") return 1;
else if(typeof right[index] === "undefined") return -1;
else if(typeof left[index] === "object" && typeof right[index] === "object") {
let a = compareArrays(left[index], right[index]);
if(a === 1 || a === -1) return a;
}
else if(typeof left[index] === "object" && typeof right[index] === "number") {
let a = compareArrays(left[index], [right[index]]);
if(a === 1 || a === -1) return a;
}
else if(typeof left[index] === "number" && typeof right[index] === "object") {
let a = compareArrays([left[index]], right[index]);
if(a === 1 || a === -1) return a;
}
else if(typeof left[index] === "number" && typeof right[index] === "number" && left[index] !== right[index]) return left[index] < right[index] ? 1 : -1
index++;
}
}
2022-12-13 21:39:11 +00:00
const inputs = [];
const beforeTwo = [];
const between = []
const afterSix = [];
2022-12-13 21:06:16 +00:00
input.split('\n\n').forEach(line => {
const pair = line.split('\n');
2022-12-13 21:39:11 +00:00
inputs.push(pair[0])
inputs.push(pair[1])
2022-12-13 21:06:16 +00:00
});
2022-12-13 21:39:11 +00:00
inputs.forEach(i => {
if(compareArrays(JSON.parse(i), JSON.parse("[[2]]")) === 1) beforeTwo.push(i);
else if(compareArrays(JSON.parse("[[6]]"), JSON.parse(i)) === 1) afterSix.push(i);
else between.push(i)
})
console.log((beforeTwo.length + 1) * (beforeTwo.length + between.length + 2))