This commit is contained in:
Alexander Gehrke 2021-12-13 12:31:59 +01:00
parent 537ec6de18
commit a0f7bd3939
5 changed files with 78 additions and 1 deletions

View file

@ -0,0 +1,7 @@
start-A
start-b
A-c
A-b
b-d
A-end
b-end

View file

@ -0,0 +1,10 @@
dc-end
HN-start
start-kj
dc-start
dc-HN
LN-dc
HN-end
kj-sa
kj-HN
kj-dc

View file

@ -0,0 +1,18 @@
fs-end
he-DX
fs-he
start-DX
pj-DX
end-zg
zg-sl
zg-pj
pj-he
RW-he
fs-DX
pj-RW
zg-RW
start-pj
he-WI
zg-he
pj-fs
start-RW

22
input/2021/day12.txt Normal file
View file

@ -0,0 +1,22 @@
xx-xh
vx-qc
cu-wf
ny-LO
cu-DR
start-xx
LO-vx
cu-LO
xx-cu
cu-ny
xh-start
qc-DR
vx-AP
end-LO
ny-DR
vx-end
DR-xx
start-DR
end-ny
ny-xx
xh-DR
cu-xh

View file

@ -3,4 +3,24 @@ package de.qwertyuiop.aoc.`2021`
import de.qwertyuiop.aoc.lib.* import de.qwertyuiop.aoc.lib.*
import cats.*, cats.implicits.given import cats.*, cats.implicits.given
def day12(using InputSource): Unit = ??? def day12(using InputSource): Unit =
val edges = input(_.splitOnce("-").getOrElse(sys.error("Invalid input")))
.flatMap((s,t) => List((s,t), (t,s)))
.groupMapReduce(_(0))(e => Set(e(1)))(_ | _)
def countPaths(edges: Map[String, Set[String]], allowSmallDoubleVisit: Boolean): Int=
def rec(pathHead: String, visitedSmall: Set[String], canStillDoubleVisit: Boolean): Int=
val neighbours = edges(pathHead).filter(n => n != "start" && (canStillDoubleVisit || !visitedSmall.contains(n)))
neighbours.toVector.map(n =>
if n == "end" then 1
else if n.head.isLower then
rec(n, visitedSmall + n, !visitedSmall.contains(n) && canStillDoubleVisit)
else
rec(n, visitedSmall, canStillDoubleVisit)
).sum
rec("start", Set("start"), allowSmallDoubleVisit)
val allPaths = countPaths(edges, false)
val allPathsWithDoubleVisit = countPaths(edges, true)
println(s"Found ${allPaths} paths with single visits on small caves only")
println(s"Found ${allPathsWithDoubleVisit} paths with one double visit")