advent-of-code/2022/13/index.js
2022-12-12 23:27:24 -08:00

58 lines
1.4 KiB
JavaScript

const fs = require('fs/promises');
const sum = (arr) => arr.reduce((total, num) => total+num, 0);
const arrayOfLength = n => Array.from(Array(n).keys()).map(() => {});
const main = async () => {
const input = (await fs.readFile("./input.txt", 'utf8')).split("\n\n").map(x => x.split("\n").map(x => eval(x)));
let orderedSum = 0;
const evaluatePair = ((l, r) => {
console.log("Compare items ----- \n ->", l, "\n ->", r);
if(typeof l === "undefined") {
console.log("l ran out");
return true;
}
if(typeof r === "undefined") {
console.log("r ran out");
return false;
}
if(typeof l === "number") {
if(typeof r === "number") {
if(l === r) {return undefined;}
console.log("l to r");
return l < r;
}
l = [l];
}
if(typeof r === "number") {
r = [r]
}
for(let [i, subL] of Object.entries(l)) {
const result = evaluatePair(subL, r[i]);
if(typeof result !== "undefined") {
return result
}
};
if(l.length < r.length) {
return true;
}
});
input.forEach(([l, r], i) => {
console.log("Evaluate ", i + 1);
const result = evaluatePair(l, r);
console.log("Evaluate ", i + 1, " as ", result);
if(result || typeof result === "undefined") {
orderedSum += i + 1;
}
});
console.log(orderedSum);
}
main();