Skip to content

Commit

Permalink
day 12
Browse files Browse the repository at this point in the history
  • Loading branch information
LighthouseKeeperYN committed Dec 12, 2021
1 parent ad5f3dc commit 9646d9f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
26 changes: 26 additions & 0 deletions 2021/12-input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
start-YY
av-rz
rz-VH
fh-av
end-fh
sk-gp
ae-av
YY-gp
end-VH
CF-qz
qz-end
qz-VG
start-gp
VG-sk
rz-YY
VH-sk
rz-gp
VH-av
VH-fh
sk-rz
YY-sk
av-gp
rz-qz
VG-start
sk-fh
VG-av
48 changes: 48 additions & 0 deletions 2021/12.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { getInput } from './lib'

const input = getInput(12).map(connection => connection.split('-'))

const part2 = (input) => {
let map: Map<string, string[]> = new Map()
for (let [from, to] of input) {
if (map.has(from)) {
map.get(from).push(to)
} else {
map.set(from, [to])
}

if (map.has(to)) {
map.get(to).push(from)
} else {
map.set(to, [from])
}
}

let paths = 0
const smallRevisited = (history: string[]) => {
let smalls = history.filter(cave => cave === cave.toLowerCase())
return smalls.some(small => history.filter(cave => cave === small).length >= 2)
}

const walk = (current: string, history: string[]) => {
if (current === 'end') return paths++

for (let cave of map.get(current)) {
if (cave === 'start') continue

if (
cave === cave.toLowerCase()
&& history.includes(cave)
&& smallRevisited(history)
) continue

walk(cave, history.concat(cave))
}
}

walk('start', [])

return paths
}

console.log(part2(input))

0 comments on commit 9646d9f

Please sign in to comment.