This commit is contained in:
Joshua Seigler 2021-12-12 00:44:11 -05:00
parent 1c4a3cfcd8
commit 0a46d00d1e
4 changed files with 111 additions and 12 deletions

View file

@ -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
}
},
{

View file

@ -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
```
<!--/RESULTS-->

9
src/day12/README.md Normal file
View file

@ -0,0 +1,9 @@
# 🎄 Advent of Code 2021 - day 12 🎄
## Info
Task description: [link](https://adventofcode.com/2021/day/12)
## Notes
...

90
src/day12/index.js Normal file
View file

@ -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,
})