day(09): refactor
- introduce ‹validAdjacentIndices› - clean up DFS - remove debugging println - fix bug of mapping over values instead of indices Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
28f318fbed
commit
1cfee94e2a
1 changed files with 14 additions and 21 deletions
|
@ -3,39 +3,34 @@ package year2021.day09
|
||||||
import readInput
|
import readInput
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
|
|
||||||
fun getRiskLevel(input: List<List<Int>>, y: Int, x: Int): Int = listOf(
|
fun validAdjacentIndices(input: List<List<Int>>, y: Int, x: Int): Iterable<Pair<Int, Int>> = listOf(
|
||||||
Pair(y - 1, x),
|
Pair(y - 1, x),
|
||||||
Pair(y + 1, x),
|
Pair(y + 1, x),
|
||||||
Pair(y, x + 1),
|
Pair(y, x + 1),
|
||||||
Pair(y, x - 1)
|
Pair(y, x - 1)
|
||||||
).filter { (y0, x0) -> y0 >= 0 && y0 < input.size && x0 >= 0 && x0 < input[y0].size }
|
).filter { (y0, x0) -> y0 >= 0 && y0 < input.size && x0 >= 0 && x0 < input[y0].size }
|
||||||
|
|
||||||
|
fun getRiskLevel(input: List<List<Int>>, y: Int, x: Int): Int = validAdjacentIndices(input, y, x)
|
||||||
.map { (y0, x0) -> input[y0][x0] }
|
.map { (y0, x0) -> input[y0][x0] }
|
||||||
.all { it > input[y][x] }.let { isLowPoint -> if (isLowPoint) input[y][x] + 1 else 0 }
|
.all { it > input[y][x] }
|
||||||
|
.let { isLowPoint -> if (isLowPoint) input[y][x] + 1 else 0 }
|
||||||
|
|
||||||
fun part1(input: List<List<Int>>): Int = input
|
fun part1(input: List<List<Int>>): Int = input
|
||||||
.mapIndexed { y, row ->
|
.mapIndexed { y, row ->
|
||||||
row.mapIndexed { x, col ->
|
row.indices.map { x ->
|
||||||
getRiskLevel(input, y, x)
|
getRiskLevel(input, y, x)
|
||||||
}
|
}
|
||||||
}.sumOf { row -> row.sum() }
|
}.sumOf { row -> row.sum() }
|
||||||
|
|
||||||
fun dfs(input: MutableList<MutableList<Int>>, y: Int, x: Int): Int {
|
fun dfs(input: MutableList<MutableList<Int>>, y: Int, x: Int): Int = when (input[y][x]) {
|
||||||
if (input[y][x] != 0) {
|
0 -> {
|
||||||
return 0
|
// mark as visited
|
||||||
|
input[y][x] = -1
|
||||||
|
|
||||||
|
// check adjacent
|
||||||
|
1 + validAdjacentIndices(input, y, x).sumOf { (y0, x0) -> dfs(input, y0, x0) }
|
||||||
}
|
}
|
||||||
|
else -> 0
|
||||||
input[y][x] = -1
|
|
||||||
|
|
||||||
var total = 1
|
|
||||||
listOf(
|
|
||||||
Pair(y - 1, x),
|
|
||||||
Pair(y + 1, x),
|
|
||||||
Pair(y, x + 1),
|
|
||||||
Pair(y, x - 1)
|
|
||||||
).filter { (y0, x0) -> y0 >= 0 && y0 < input.size && x0 >= 0 && x0 < input[y0].size }
|
|
||||||
.forEach { (y0, x0) -> total += dfs(input, y0, x0) }
|
|
||||||
|
|
||||||
return total;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun part2(input: List<List<Int>>): Int {
|
fun part2(input: List<List<Int>>): Int {
|
||||||
|
@ -56,8 +51,6 @@ fun part2(input: List<List<Int>>): Int {
|
||||||
}
|
}
|
||||||
|
|
||||||
sizes.sortDescending()
|
sizes.sortDescending()
|
||||||
println(sizes)
|
|
||||||
|
|
||||||
return sizes.take(3).fold(1) { product, n -> n * product }
|
return sizes.take(3).fold(1) { product, n -> n * product }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue