-
Notifications
You must be signed in to change notification settings - Fork 2
/
day12.js
52 lines (47 loc) · 1.48 KB
/
day12.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
'use strict';
const fs = require('fs');
const cl = console.log;
fs.readFile('day12.txt', 'utf-8', (err, input) => {
if (err) throw err;
let pairs = input.trim().split(/\r?\n/).map(ln => ln.split('-'));
cl(passageParsing(pairs, false));
cl(passageParsing(pairs, true));
});
function passageParsing(pairs, mayRevist) {
let connections = findConnections(pairs);
let pathCount = 0;
for (let _ of findPaths(connections, ['start'], !mayRevist)) {
pathCount++;
}
return pathCount;
}
function findConnections(pairs) {
let connections = new Map();
pairs.forEach(([left, right]) => {
if (!connections.has(left)) { connections.set(left, new Set()) };
if (!connections.has(right)) { connections.set(right, new Set()) };
connections.get(left).add(right);
connections.get(right).add(left);
});
return connections;
}
function* findPaths(connections, path, alreadyRevisited) {
let here = path.at(-1);
if (here === 'end') yield path;
for (let next of connections.get(here)) {
let goNext = false;
let haveRevisited = alreadyRevisited;
if (/[A-Z]/.test(next) || !path.includes(next)) {
goNext = true;
}
else if (!alreadyRevisited && next.length <= 2) {
haveRevisited = true;
goNext = true;
}
if (goNext) {
path.push(next);
yield* findPaths(connections, path, haveRevisited);
}
}
path.pop();
}