tweaks
This commit is contained in:
parent
8de4107b95
commit
c1f6916c8b
@ -2,53 +2,51 @@ import fs from "fs-extra";
|
|||||||
import fp from "lodash/fp.js";
|
import fp from "lodash/fp.js";
|
||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
|
|
||||||
import * as p1 from './part1.mjs';
|
import * as p1 from "./part1.mjs";
|
||||||
|
|
||||||
export function solution(graph) {
|
export function solution(graph) {
|
||||||
let paths = 0;
|
let paths = 0;
|
||||||
|
|
||||||
graph.forEachNode( node => {
|
graph.forEachNode((node) => {
|
||||||
graph.setNodeAttribute( node, 'nbrVisits', 0 )
|
graph.setNodeAttribute(node, "nbrVisits", 0);
|
||||||
} );
|
});
|
||||||
|
|
||||||
const left = [
|
const left = [{ graph, where: "start" }];
|
||||||
{ graph, where: 'start' }
|
|
||||||
];
|
|
||||||
|
|
||||||
while( left.length > 0 ) {
|
while (left.length > 0) {
|
||||||
const position = left.shift();
|
const position = left.shift();
|
||||||
|
|
||||||
if( position.where === 'end' ) {
|
if (position.where === "end") {
|
||||||
paths++;
|
paths++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const canVisit = (graph,node) => {
|
const canVisit = (graph, node) => {
|
||||||
if(node === 'start') return false;
|
if (node === "start") return false;
|
||||||
if( node === node.toUpperCase() ) return true;
|
if (node === node.toUpperCase()) return true;
|
||||||
const nbrVisits =graph.getNodeAttribute(node,'nbrVisits');
|
const nbrVisits = graph.getNodeAttribute(node, "nbrVisits");
|
||||||
|
|
||||||
if(nbrVisits ===0 ) return true;
|
if (nbrVisits === 0) return true;
|
||||||
if(nbrVisits>1) return false;
|
if (nbrVisits > 1) return false;
|
||||||
return !graph.hasAttribute('revisited');
|
return !graph.hasAttribute("revisited");
|
||||||
}
|
};
|
||||||
|
|
||||||
for( const n of position.graph.neighbors(position.where) ) {
|
|
||||||
if( !canVisit( position.graph, n ) ) continue;
|
|
||||||
|
|
||||||
|
for (const n of position.graph
|
||||||
|
.neighbors(position.where)
|
||||||
|
.filter((n) => canVisit(position.graph, n))) {
|
||||||
let newGraph = position.graph;
|
let newGraph = position.graph;
|
||||||
|
|
||||||
if( n === n.toLowerCase() ) {
|
if (n === n.toLowerCase()) {
|
||||||
newGraph = newGraph.copy();
|
newGraph = newGraph.copy();
|
||||||
newGraph.setNodeAttribute(
|
newGraph.updateNodeAttribute(
|
||||||
n, 'nbrVisits', 1 + newGraph.getNodeAttribute(n,'nbrVisits')
|
n,
|
||||||
|
"nbrVisits",
|
||||||
|
fp.add(1)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (newGraph.getNodeAttribute(n, "nbrVisits") === 2) {
|
||||||
if( newGraph.getNodeAttribute(n,'nbrVisits') === 2 ) {
|
newGraph.setAttribute("revisited", n);
|
||||||
newGraph.setAttribute('revisited',n);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
left.unshift({
|
left.unshift({
|
||||||
@ -56,7 +54,6 @@ export function solution(graph) {
|
|||||||
where: n,
|
where: n,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return paths;
|
return paths;
|
||||||
|
Loading…
Reference in New Issue
Block a user