From 8de4107b9594c61c1b04be91e52c40bce20ebeff Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 12 Dec 2021 13:19:18 -0500 Subject: [PATCH 1/2] part 2 --- 2021/12/part1.mjs | 51 ++++++++++++++++++++++++++++++++++++++ 2021/12/part2.mjs | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 2021/12/sample | 7 ++++++ 2021/12/test.mjs | 20 +++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 2021/12/part1.mjs create mode 100644 2021/12/part2.mjs create mode 100644 2021/12/sample create mode 100644 2021/12/test.mjs diff --git a/2021/12/part1.mjs b/2021/12/part1.mjs new file mode 100644 index 0000000..8767932 --- /dev/null +++ b/2021/12/part1.mjs @@ -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; +} diff --git a/2021/12/part2.mjs b/2021/12/part2.mjs new file mode 100644 index 0000000..eaf12ba --- /dev/null +++ b/2021/12/part2.mjs @@ -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; +} diff --git a/2021/12/sample b/2021/12/sample new file mode 100644 index 0000000..6fd8c41 --- /dev/null +++ b/2021/12/sample @@ -0,0 +1,7 @@ +start-A +start-b +A-c +A-b +b-d +A-end +b-end diff --git a/2021/12/test.mjs b/2021/12/test.mjs new file mode 100644 index 0000000..196547b --- /dev/null +++ b/2021/12/test.mjs @@ -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); +}); From c1f6916c8ba9e0e0c67bbd810aab6c8e547c48aa Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 12 Dec 2021 13:24:41 -0500 Subject: [PATCH 2/2] tweaks --- 2021/12/part2.mjs | 53 ++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/2021/12/part2.mjs b/2021/12/part2.mjs index eaf12ba..8dfa908 100644 --- a/2021/12/part2.mjs +++ b/2021/12/part2.mjs @@ -2,53 +2,51 @@ import fs from "fs-extra"; import fp from "lodash/fp.js"; import _ from "lodash"; -import * as p1 from './part1.mjs'; +import * as p1 from "./part1.mjs"; export function solution(graph) { let paths = 0; - graph.forEachNode( node => { - graph.setNodeAttribute( node, 'nbrVisits', 0 ) - } ); + graph.forEachNode((node) => { + graph.setNodeAttribute(node, "nbrVisits", 0); + }); - const left = [ - { graph, where: 'start' } - ]; + const left = [{ graph, where: "start" }]; - while( left.length > 0 ) { + while (left.length > 0) { const position = left.shift(); - if( position.where === 'end' ) { + 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'); + 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; + 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() ) { + if (n === n.toLowerCase()) { newGraph = newGraph.copy(); - newGraph.setNodeAttribute( - n, 'nbrVisits', 1 + newGraph.getNodeAttribute(n,'nbrVisits') + newGraph.updateNodeAttribute( + n, + "nbrVisits", + fp.add(1) ); - - if( newGraph.getNodeAttribute(n,'nbrVisits') === 2 ) { - newGraph.setAttribute('revisited',n); + if (newGraph.getNodeAttribute(n, "nbrVisits") === 2) { + newGraph.setAttribute("revisited", n); } - } left.unshift({ @@ -56,7 +54,6 @@ export function solution(graph) { where: n, }); } - } return paths;