Merge branch '2021-12'

This commit is contained in:
Yanick Champoux 2021-12-12 13:24:46 -05:00
commit f3b275752e
4 changed files with 138 additions and 0 deletions

51
2021/12/part1.mjs Normal file
View 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;
}

60
2021/12/part2.mjs Normal file
View File

@ -0,0 +1,60 @@
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)
.filter((n) => canVisit(position.graph, n))) {
let newGraph = position.graph;
if (n === n.toLowerCase()) {
newGraph = newGraph.copy();
newGraph.updateNodeAttribute(
n,
"nbrVisits",
fp.add(1)
);
if (newGraph.getNodeAttribute(n, "nbrVisits") === 2) {
newGraph.setAttribute("revisited", n);
}
}
left.unshift({
graph: newGraph,
where: n,
});
}
}
return paths;
}

7
2021/12/sample Normal file
View 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
View 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);
});