adventOfCode2024/5/index.js
2025-06-13 20:13:44 +12:00

52 lines
No EOL
1.5 KiB
JavaScript

const fs = require("fs");
const inputRules = fs.readFileSync("input1.txt", "utf-8");
const inputPages = fs.readFileSync("input2.txt", "utf-8");
const rulesMapForward = new Map();
const rulesMapBackward = new Map();
function loadTree() {
//split the rules into an array of lines
const inputRulesArr = inputRules.split("\n");
//load forward rules
for (line of inputRulesArr) {
//split the lines into a first and second part
const [firstPage, secondPage] = line.split("|");
//if the rule doesnt exist yet create it
if (!rulesMapForward.has(firstPage)) {
rulesMapForward.set(firstPage, []);
}
//add the second page into a array stored in the map
const prevArray = rulesMapForward.get(firstPage);
rulesMapForward.set(firstPage, [...prevArray, secondPage]);
}
//load backword rules
for (line of inputRulesArr) {
//split the lines into a first and second part
const [firstPage, secondPage] = line.split("|");
//if the rule doesnt exist yet create it
if (!rulesMapBackward.has(secondPage)) {
rulesMapBackward.set(secondPage, []);
}
//add the second page into a array stored in the map
const prevArray = rulesMapBackward.get(secondPage);
rulesMapBackward.set(secondPage, [...prevArray, firstPage]);
}
}
loadTree();
// console.log(rulesMapForward);
// console.log("------------------");
// console.log(rulesMapBackward);
const inputPagesArr =inputPages.split("\n")
for (book of inputPagesArr){
const pages = book.split(",")
for (let page =0;page<pages.length;page++){
}
}