Advent of Code 2021 day 12
12/12/2021, 5:00 AMMichael Böiers
12/12/2021, 6:45 AMJakub GwĂłĆșdĆș
12/12/2021, 6:54 AMDavid Whittaker
12/12/2021, 7:36 AMmap
Michael Böiers
12/12/2021, 7:40 AMKiet
12/12/2021, 8:53 AMephemient
12/12/2021, 9:33 AMInt
as a bitset again (including using the high bit for part 2)Michael de Kaste
12/12/2021, 12:24 PMMarcin Wisniowski
12/12/2021, 1:14 PMPaul Woitaschek
12/12/2021, 4:22 PMMichael Böiers
12/12/2021, 4:33 PMPaul Woitaschek
12/12/2021, 4:37 PMMichael Böiers
12/12/2021, 5:01 PMMichael de Kaste
12/12/2021, 5:02 PMMichael Böiers
12/12/2021, 5:04 PMDavid Whittaker
12/12/2021, 7:16 PMMichael Böiers
12/12/2021, 7:24 PMDavid Whittaker
12/12/2021, 7:31 PMMichael Böiers
12/12/2021, 7:34 PMfun Set<Set<String>>.findExit(paths: List<String> = listOf("start"), twiceAllowed: Boolean): List<List<String>> {
val here = paths.last().takeUnless { it == "end" } ?: return listOf(paths)
val canVisitSmallTwice = twiceAllowed && paths.filter { it.all(Char::isLowerCase) }.let { it == it.distinct() }
return mapNotNull { rule -> if (rule.contains(here)) rule.single { it != here } else null }
.filter { if (canVisitSmallTwice) it != "start" else it.any(Char::isUpperCase) || !paths.contains(it) }
.flatMap { findExit(paths.plus(it), twiceAllowed) }
}
Michael Böiers
12/12/2021, 7:35 PMMichael Böiers
12/12/2021, 10:53 PMfun Map<String, Set<String>>.explore(paths: List<String> = listOf("start"), twiceAllowed: Boolean): List<List<String>> {
val here = paths.last().takeUnless { it == "end" } ?: return listOf(paths)
val canVisitSmallTwice = twiceAllowed && paths.filter { it == it.lowercase() }.let { it == it.distinct() }
return getValue(here)
.filter { if (canVisitSmallTwice) it != "start" else it == it.uppercase() || !paths.contains(it) }
.flatMap { explore(paths.plus(it), twiceAllowed) }
}
Dan Fingal-Surma
12/13/2021, 12:44 AMDan Fingal-Surma
12/13/2021, 12:56 AMphldavies
12/13/2021, 1:02 AMDan Fingal-Surma
12/13/2021, 1:27 AMNir
12/13/2021, 2:45 AMephemient
12/13/2021, 3:27 AMNir
12/13/2021, 3:29 AMNir
12/13/2021, 3:29 AMtypealias Graph = Map<String, List<String>>
fun getInputs(): Graph = (aocDataDir / "2021" / "day12.txt").useLines { lines ->
mutableMapOf<String, MutableList<String>>().apply {
lines.forEach { line ->
val (node1, node2) = line.split("-")
getOrPut(node1) { mutableListOf<String>() }.add(node2)
getOrPut(node2) { mutableListOf<String>() }.add(node1)
}
}
}
fun Graph.countPaths(canVisitTwice: Boolean): Long {
val smallCavesVisited = mutableMapOf<String, Int>()
fun recurse(hasSecondVisit: Boolean, nodeName: String): Long {
if (nodeName == "end") {
return 1
}
val smallTimesVisited = smallCavesVisited.getOrDefault(nodeName, 0)
if (smallTimesVisited == 2 || (smallTimesVisited == 1 && (nodeName == "start" || !hasSecondVisit))) {
return 0
}
if (nodeName.toLowerCase() == nodeName) {
smallCavesVisited.update(nodeName) { (it ?: 0) + 1 }
}
val stillHasSecondVisit = hasSecondVisit && smallTimesVisited == 0
return getValue(nodeName).sumOf { recurse(stillHasSecondVisit, it) }.also {
smallCavesVisited[nodeName]?.let { smallCavesVisited[nodeName] = it - 1 }
}
}
return recurse(canVisitTwice, "start")
}
ephemient
12/13/2021, 3:30 AM