Day 12
This commit is contained in:
parent
537ec6de18
commit
a0f7bd3939
7
input/2021/day12-sample1.txt
Normal file
7
input/2021/day12-sample1.txt
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
start-A
|
||||||
|
start-b
|
||||||
|
A-c
|
||||||
|
A-b
|
||||||
|
b-d
|
||||||
|
A-end
|
||||||
|
b-end
|
10
input/2021/day12-sample2.txt
Normal file
10
input/2021/day12-sample2.txt
Normal 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
|
18
input/2021/day12-sample3.txt
Normal file
18
input/2021/day12-sample3.txt
Normal 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
22
input/2021/day12.txt
Normal 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
|
|
@ -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")
|
||||||
|
|
Loading…
Reference in a new issue