mirror of
https://github.com/seigler/aoc2021
synced 2025-07-27 01:16:09 +00:00
day 12
This commit is contained in:
parent
1c4a3cfcd8
commit
0a46d00d1e
4 changed files with 111 additions and 12 deletions
|
@ -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
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
12
README.md
12
README.md
|
@ -22,7 +22,7 @@
|
|||
[](src/day09)
|
||||
[](src/day10)
|
||||
[](src/day11)
|
||||

|
||||
[](src/day12)
|
||||

|
||||

|
||||

|
||||
|
@ -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
9
src/day12/README.md
Normal 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
90
src/day12/index.js
Normal 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,
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue