-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay03TobogganTrajectory.kt
39 lines (28 loc) · 1.04 KB
/
Day03TobogganTrajectory.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
package adventofcode.year2020
import adventofcode.Puzzle
import adventofcode.PuzzleInput
import adventofcode.common.product
class Day03TobogganTrajectory(customInput: PuzzleInput? = null) : Puzzle(customInput) {
private val treeMap by lazy { input.lines() }
override fun partOne() = treeMap.countTrees(Slope(3, 1))
override fun partTwo() =
listOf(Slope(1, 1), Slope(3, 1), Slope(5, 1), Slope(7, 1), Slope(1, 2))
.map { treeMap.countTrees(it) }
.product()
companion object {
private data class Slope(val dx: Long, val dy: Long)
private fun List<String>.countTrees(slope: Slope): Long {
var counter = 0L
var x = 0L
var y = 0L
while (y < size) {
val currentLine = this[y.toInt()].toCharArray()
val field = currentLine[x.toInt() % currentLine.size]
if (field == '#') counter++
x += slope.dx
y += slope.dy
}
return counter
}
}
}