Day12 2021
This commit is contained in:
parent
73235b3e44
commit
ed3c7f3a04
57
2021/src/main/kotlin/Day12.kt
Normal file
57
2021/src/main/kotlin/Day12.kt
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
23
2021/src/main/resources/day12.txt
Normal file
23
2021/src/main/resources/day12.txt
Normal 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
|
||||||
19
2021/src/test/kotlin/Day12Test.kt
Normal file
19
2021/src/test/kotlin/Day12Test.kt
Normal 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
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user