1
0

Day12 2021

This commit is contained in:
Hubert Van De Walle 2021-12-12 15:24:22 +01:00
parent 73235b3e44
commit ed3c7f3a04
3 changed files with 99 additions and 0 deletions

View File

@ -0,0 +1,57 @@
package be.vandewalleh.aoc.days
import be.vandewalleh.aoc.utils.BaseDay
import be.vandewalleh.aoc.utils.input.Day
@Day
class Day12 : BaseDay() {
private val map by lazy {
HashMap<String, MutableSet<String>>().also { map ->
input.lines.value.map { it.split("-") }
.flatMap { (a, b) -> listOf(listOf(a, b), listOf(b, a)) }
.filter { (_, b) -> b != "start" }
.forEach { (a, b) -> map.computeIfAbsent(a) { HashSet() }.add(b) }
}
}
override fun part1(): Any {
val stack = map["start"]!!.map { listOf("start", it) }.toMutableList()
var i = 0
while (stack.isNotEmpty()) {
val path = stack.removeLast()
val node = path.last()
for (next in map[node]!!) {
when {
next == "end" -> i++
next[0].isUpperCase() -> stack.add(path + next)
else -> if (next !in path) stack.add(path + next)
}
}
}
return i
}
override fun part2(): Any {
val stack = map["start"]!!.map { listOf("start", it) }.toMutableList()
var i = 0
while (stack.isNotEmpty()) {
val path = stack.removeLast()
val node = path.last()
for (next in map[node]!!) {
when {
next == "end" -> i++
next[0].isUpperCase() -> stack.add(path + next)
else -> {
val hasTwoSmallCaves = path.filter { it[0].isLowerCase() }
.any { cur -> path.count { it == cur } == 2 }
if (!hasTwoSmallCaves || next !in path) stack.add(path + next)
}
}
}
}
return i
}
}

View File

@ -0,0 +1,23 @@
pn-TY
rp-ka
az-aw
al-IV
pn-co
end-rp
aw-TY
rp-pn
al-rp
end-al
IV-co
end-TM
co-TY
TY-ka
aw-pn
aw-IV
pn-IV
IV-ka
TM-rp
aw-PD
start-IV
start-co
start-pn

View File

@ -0,0 +1,19 @@
package be.vandewalleh.aoc.days
class Day12Test : BaseDayTest() {
override val example = """
start-A
start-b
A-c
A-b
b-d
A-end
b-end
""".trimIndent()
override val part1Example = 10
override val part1Answer = 4413
override val part2Example = 36
override val part2Answer = 118803
}