diff --git a/.aocrunner.json b/.aocrunner.json index 6c147be..c5f7fb5 100644 --- a/.aocrunner.json +++ b/.aocrunner.json @@ -167,16 +167,16 @@ }, { "part1": { - "solved": false, - "result": null, + "solved": true, + "result": "5576", "attempts": [], - "time": null + "time": 39.91 }, "part2": { - "solved": false, - "result": null, + "solved": true, + "result": "152837", "attempts": [], - "time": null + "time": 562.6 } }, { diff --git a/README.md b/README.md index d0ed478..6570e21 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ [![Day](https://badgen.net/badge/09/%E2%98%85%E2%98%85/green)](src/day09) [![Day](https://badgen.net/badge/10/%E2%98%85%E2%98%85/green)](src/day10) [![Day](https://badgen.net/badge/11/%E2%98%85%E2%98%85/green)](src/day11) -![Day](https://badgen.net/badge/12/%E2%98%86%E2%98%86/gray) +[![Day](https://badgen.net/badge/12/%E2%98%85%E2%98%85/green)](src/day12) ![Day](https://badgen.net/badge/13/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/14/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/15/%E2%98%86%E2%98%86/gray) @@ -146,9 +146,9 @@ Both parts: 11.29ms ``` Day 12 -Time part 1: - -Time part 2: - -Both parts: - +Time part 1: 39.91ms +Time part 2: 562.6ms +Both parts: 602.51ms ``` ``` @@ -243,8 +243,8 @@ Both parts: - ``` ``` -Total stars: 22/50 -Total time: 454.25ms +Total stars: 24/50 +Total time: 1056.76ms ``` diff --git a/src/day12/README.md b/src/day12/README.md new file mode 100644 index 0000000..f43cc54 --- /dev/null +++ b/src/day12/README.md @@ -0,0 +1,9 @@ +# 🎄 Advent of Code 2021 - day 12 🎄 + +## Info + +Task description: [link](https://adventofcode.com/2021/day/12) + +## Notes + +... \ No newline at end of file diff --git a/src/day12/index.js b/src/day12/index.js new file mode 100644 index 0000000..79d1321 --- /dev/null +++ b/src/day12/index.js @@ -0,0 +1,90 @@ +import run from "aocrunner" + +const parseInput = (rawInput) => rawInput.split`\n`.map((x) => x.split`-`) + +const part1 = (rawInput) => { + let input = parseInput(rawInput) + input = input.concat(input.map(([from, to]) => [to, from])) + function countPaths(history) { + const here = history[history.length - 1] + if (here === "end") { + // console.log(history.join`,`) + return 1 + } + const exits = [ + ...input.filter( + ([from, to]) => + from === here && (/[A-Z]+/.test(to) || !history.includes(to)), + ), + ] + return exits.reduce( + (acc, [from, to]) => acc + countPaths([...history, to]), + 0, + ) + } + return countPaths(["start"]) +} + +const part2 = (rawInput) => { + let input = parseInput(rawInput) + input = input.concat(input.map(([from, to]) => [to, from])) + function countPaths(history, haveDoubled = false) { + const here = history[history.length - 1] + if (here === "end") { + // console.log(history.join`,`) + return 1 + } + const exits = [ + ...input.filter( + ([from, to]) => + from === here && + to !== "start" && + (!haveDoubled || /[A-Z]+/.test(to) || !history.includes(to)), + ), + ] + return exits.reduce( + (acc, [from, to]) => + acc + + countPaths( + [...history, to], + haveDoubled || (/[a-z]+/.test(to) && history.includes(to)), + ), + 0, + ) + } + return countPaths(["start"]) +} + +run({ + part1: { + tests: [ + { + input: `start-A +start-b +A-c +A-b +b-d +A-end +b-end`, + expected: 10, + }, + ], + solution: part1, + }, + part2: { + tests: [ + { + input: `start-A +start-b +A-c +A-b +b-d +A-end +b-end`, + expected: 36, + }, + ], + solution: part2, + }, + trimTestInputs: true, +})