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+/).map(x => eval(x)); 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.push([[2]], [[6]]); input.sort((l, r) => { const result = evaluatePair(l, r); console.log("SORT RESULT: ", result); return result || typeof result === "undefined" ? -1 : 1; }); console.log("Input: ", input); const firstIndex = input.findIndex(x => JSON.stringify(x) === "[[2]]") + 1; const secondIndex = input.findIndex(x => JSON.stringify(x) === "[[6]]") + 1; console.log(firstIndex * secondIndex); } main();