forward and back maps created

This commit is contained in:
Seth Samuel 2025-04-29 23:39:56 +00:00
parent 6ec60b3bd8
commit e1ca53ecb0
6 changed files with 1454 additions and 1 deletions

43
5/index.js Normal file
View file

@ -0,0 +1,43 @@
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);