43 lines
1.4 KiB
JavaScript
43 lines
1.4 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);
|