-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.js
52 lines (41 loc) · 1.02 KB
/
day08.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { getLines } from "./helpers";
const example = `LLR
AAA = (BBB, BBB)
BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)`;
const exampleLines = example.split("\n");
const lines = getLines("inputs/day08.txt");
function getAdjList(lines) {
const result = {};
lines.forEach((line) => {
const [node, adjacentNodesStr] = line.split(" = ");
const [left, right] = adjacentNodesStr.replace(/[\(\)]/g, "").split(", ");
result[node] = [left, right];
});
return result;
}
function getInstructions(lines) {
return lines[0];
}
function getGraphLines(lines) {
return lines.slice(2);
}
function getCount(adjList, instructions) {
let currNode = "AAA";
let i = 0;
let count = 0;
while (currNode !== "ZZZ") {
if (i >= instructions.length) {
i = 0;
}
const dir = instructions[i];
currNode = adjList[currNode][dir === "L" ? 0 : 1];
i += 1;
count += 1;
}
return count;
}
function part1(lines) {
return getCount(getAdjList(getGraphLines(lines)), getInstructions(lines));
}
console.log(part1(lines));