1
0
Files
Advent-of-Code/days/src/main/kotlin/Day03.kt
T
2020-12-03 08:22:44 +01:00

46 lines
1.0 KiB
Kotlin

package be.vandewalleh.aoc.days
import be.vandewalleh.aoc.utils.input.Day
import be.vandewalleh.aoc.utils.input.Input
import be.vandewalleh.aoc.utils.input.Lines
import be.vandewalleh.aoc.utils.input.createDay
data class Slope(val x: Int, val y: Int)
@Day(3)
class Day03(@Lines val input: Input<List<String>>) {
fun part1(slope: Slope = Slope(x = 3, y = 1)): Int {
val grid = input.value
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 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 { part1(it).toLong() }
.reduce { acc, trees -> acc * trees }
}
fun main() = with(createDay<Day03>()) {
println(part1())
println(part2())
}