part 2
This commit is contained in:
parent
86c46e467b
commit
8de4107b95
51
2021/12/part1.mjs
Normal file
51
2021/12/part1.mjs
Normal file
@ -0,0 +1,51 @@
|
||||
import fs from "fs-extra";
|
||||
import fp from "lodash/fp.js";
|
||||
import _ from "lodash";
|
||||
|
||||
import Graph from 'graphology';
|
||||
|
||||
|
||||
|
||||
export const processInput = (filename) => fs.readFile(filename,'utf8').then(
|
||||
content => content.split("\n").filter(x=>x)
|
||||
).then(
|
||||
lines => {
|
||||
const graph = new Graph();
|
||||
lines.map( line => line.split('-') ).forEach( edge =>
|
||||
graph.mergeEdge(...edge) );
|
||||
return graph;
|
||||
}
|
||||
|
||||
);
|
||||
|
||||
export function solution(graph) {
|
||||
let paths = 0;
|
||||
const left = [
|
||||
{ graph, where: 'start' }
|
||||
];
|
||||
|
||||
while( left.length > 0 ) {
|
||||
const position = left.shift();
|
||||
|
||||
if( position.where === 'end' ) {
|
||||
paths++;
|
||||
continue;
|
||||
}
|
||||
|
||||
let newGraph = position.graph;
|
||||
if( position.where === position.where.toLowerCase() ) {
|
||||
newGraph = position.graph.copy();
|
||||
newGraph.dropNode(position.where);
|
||||
}
|
||||
|
||||
for( const n of position.graph.neighbors(position.where) ) {
|
||||
left.unshift({
|
||||
graph: newGraph,
|
||||
where: n,
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return paths;
|
||||
}
|
63
2021/12/part2.mjs
Normal file
63
2021/12/part2.mjs
Normal file
@ -0,0 +1,63 @@
|
||||
import fs from "fs-extra";
|
||||
import fp from "lodash/fp.js";
|
||||
import _ from "lodash";
|
||||
|
||||
import * as p1 from './part1.mjs';
|
||||
|
||||
export function solution(graph) {
|
||||
let paths = 0;
|
||||
|
||||
graph.forEachNode( node => {
|
||||
graph.setNodeAttribute( node, 'nbrVisits', 0 )
|
||||
} );
|
||||
|
||||
const left = [
|
||||
{ graph, where: 'start' }
|
||||
];
|
||||
|
||||
while( left.length > 0 ) {
|
||||
const position = left.shift();
|
||||
|
||||
if( position.where === 'end' ) {
|
||||
paths++;
|
||||
continue;
|
||||
}
|
||||
|
||||
const canVisit = (graph,node) => {
|
||||
if(node === 'start') return false;
|
||||
if( node === node.toUpperCase() ) return true;
|
||||
const nbrVisits =graph.getNodeAttribute(node,'nbrVisits');
|
||||
|
||||
if(nbrVisits ===0 ) return true;
|
||||
if(nbrVisits>1) return false;
|
||||
return !graph.hasAttribute('revisited');
|
||||
}
|
||||
|
||||
for( const n of position.graph.neighbors(position.where) ) {
|
||||
if( !canVisit( position.graph, n ) ) continue;
|
||||
|
||||
let newGraph = position.graph;
|
||||
|
||||
if( n === n.toLowerCase() ) {
|
||||
newGraph = newGraph.copy();
|
||||
newGraph.setNodeAttribute(
|
||||
n, 'nbrVisits', 1 + newGraph.getNodeAttribute(n,'nbrVisits')
|
||||
);
|
||||
|
||||
|
||||
if( newGraph.getNodeAttribute(n,'nbrVisits') === 2 ) {
|
||||
newGraph.setAttribute('revisited',n);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
left.unshift({
|
||||
graph: newGraph,
|
||||
where: n,
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return paths;
|
||||
}
|
7
2021/12/sample
Normal file
7
2021/12/sample
Normal file
@ -0,0 +1,7 @@
|
||||
start-A
|
||||
start-b
|
||||
A-c
|
||||
A-b
|
||||
b-d
|
||||
A-end
|
||||
b-end
|
20
2021/12/test.mjs
Normal file
20
2021/12/test.mjs
Normal file
@ -0,0 +1,20 @@
|
||||
// https://adventofcode.com/2021/day/12
|
||||
|
||||
import tap from "tap";
|
||||
import fs from "fs-extra";
|
||||
|
||||
import * as p1 from "./part1.mjs";
|
||||
import * as p2 from "./part2.mjs";
|
||||
|
||||
const sample = p1.processInput('sample');
|
||||
const input = p1.processInput('input');
|
||||
|
||||
tap.test("part1", async (t) => {
|
||||
t.equal(p1.solution(await sample), 10);
|
||||
t.equal(p1.solution(await input), 3450);
|
||||
});
|
||||
|
||||
tap.test("part2", async (t) => {
|
||||
t.equal(p2.solution(await sample), 36);
|
||||
t.equal(p2.solution(await input), 96528);
|
||||
});
|
Loading…
Reference in New Issue
Block a user