forward and back maps created
This commit is contained in:
parent
6ec60b3bd8
commit
e1ca53ecb0
6 changed files with 1454 additions and 1 deletions
43
5/index.js
Normal file
43
5/index.js
Normal 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);
|
||||
Loading…
Add table
Add a link
Reference in a new issue