This commit is contained in:
nim-ka
2022-11-17 00:57:31 +00:00
parent 26120e2818
commit c42480ef0b
6 changed files with 133 additions and 12 deletions

42
2021/12.js Normal file
View File

@@ -0,0 +1,42 @@
// TODO: not very efficient because i was lazy
function day12(input, part2) {
let cxns = input.split("\n").map((line) => line.split("-"))
let dict = {}
for (let cxn of cxns) {
dict[cxn[0]] = [...(dict[cxn[0]] || []), cxn[1]]
dict[cxn[1]] = [...(dict[cxn[1]] || []), cxn[0]]
}
let count = 0
let paths = [["start"]]
while (paths.length) {
let newPaths = []
for (let path of paths) {
for (let next of dict[path.last]) {
if (next == "start") {
continue
}
let newPath = [...path, next]
if (newPath.count((e, i, a) => e == e.toLowerCase() && a.indexOf(e) != i) > part2) {
continue
}
if (next == "end") {
count++
} else {
newPaths.push(newPath)
}
}
}
paths = newPaths
}
return count
}