1
0
Files
Advent-of-Code/2020/src/main/kotlin/Day03.kt
T
2021-12-01 19:32:38 +01:00

40 lines
898 B
Kotlin

package be.vandewalleh.aoc.days
import be.vandewalleh.aoc.utils.input.Day
import be.vandewalleh.aoc.utils.input.Lines
@Day(3)
class Day03(@Lines val input: List<String>) {
private data class Slope(val x: Int, val y: Int)
private fun findSlope(slope: Slope): Int {
val grid = input
var trees = 0
var x = 0
var y = 0
val width = grid.first().length
while (y < grid.size - 1) {
x += slope.x
y += slope.y
if (grid[y][x % width] == '#') trees++
}
return trees
}
fun part1() = findSlope(Slope(x = 3, y = 1))
fun part2(): Long = listOf(
Slope(x = 1, y = 1),
Slope(x = 3, y = 1),
Slope(x = 5, y = 1),
Slope(x = 7, y = 1),
Slope(x = 1, y = 2),
)
.map { findSlope(it).toLong() }
.reduce { acc, trees -> acc * trees }
}