-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay08TreetopTreeHouse.kt
58 lines (51 loc) · 2.12 KB
/
Day08TreetopTreeHouse.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package adventofcode.year2022
import adventofcode.Puzzle
import adventofcode.PuzzleInput
import adventofcode.common.product
import adventofcode.year2022.Day08TreetopTreeHouse.Companion.Direction.DOWN
import adventofcode.year2022.Day08TreetopTreeHouse.Companion.Direction.LEFT
import adventofcode.year2022.Day08TreetopTreeHouse.Companion.Direction.RIGHT
import adventofcode.year2022.Day08TreetopTreeHouse.Companion.Direction.UP
class Day08TreetopTreeHouse(customInput: PuzzleInput? = null) : Puzzle(customInput) {
private val treeMap by lazy { input.lines().map { line -> line.map(Char::digitToInt) } }
override fun partOne() =
treeMap
.flatMapIndexed { y, row ->
row.mapIndexed { x, tree ->
Direction
.entries
.map { direction -> treeMap.neighbors(x, y, direction).filter { neighbor -> neighbor >= tree } }
.any(List<Int>::isEmpty)
}
}
.count { tree -> tree }
override fun partTwo() =
treeMap
.flatMapIndexed { y, row ->
row.mapIndexed { x, tree ->
Direction
.entries
.map { direction ->
val neighbors = treeMap.neighbors(x, y, direction)
when (val tallestNeighbor = neighbors.indexOfFirst { neighbor -> neighbor >= tree }) {
-1 -> neighbors.size
else -> tallestNeighbor + 1
}
}
}
}
.maxOf(List<Int>::product)
companion object {
private enum class Direction { DOWN, LEFT, RIGHT, UP }
private fun List<List<Int>>.neighbors(
x: Int,
y: Int,
direction: Direction,
) = when (direction) {
DOWN -> map { row -> row[x] }.drop(y + 1)
LEFT -> this[y].take(x).reversed()
RIGHT -> this[y].drop(x + 1)
UP -> map { row -> row[x] }.take(y).reversed()
}
}
}