-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.mjs
46 lines (41 loc) · 1.33 KB
/
tasks.mjs
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
import input from './input.mjs';
function mapConnections(input) {
const connections = {};
input.forEach((connection) => {
const [start, end] = connection.split('-');
if (!connections[start]) connections[start] = [];
if (!connections[end]) connections[end] = [];
if (end !== 'start') connections[start].push(end);
if (start !== 'start') connections[end].push(start);
});
return connections;
}
function findPaths(input, allowMultiple) {
const connections = mapConnections(input);
const queue = [['start']];
const completed = [];
while (queue.length) {
const currentPath = queue.pop();
const currentNode = currentPath.at(-1);
connections[currentNode].forEach((nextNode) => {
if (
isLowerCase(nextNode) &&
currentPath.includes(nextNode) &&
(!allowMultiple || hasTwoVisits(currentPath))
)
return;
if (nextNode === 'end') return completed.push([...currentPath, nextNode]);
queue.push([...currentPath, nextNode]);
});
}
return completed.length;
}
function hasTwoVisits(path) {
const onlySmallCaves = path.filter((node) => isLowerCase(node));
return new Set(onlySmallCaves).size !== onlySmallCaves.length;
}
function isLowerCase(letter) {
return letter.toLowerCase() === letter;
}
console.log(findPaths(input));
console.log(findPaths(input, true));