Day03
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
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 (true) {
|
||||
x += slope.x
|
||||
y += slope.y
|
||||
|
||||
if (x >= width) x -= width
|
||||
if (y >= grid.size) break
|
||||
if (grid[y][x] == '#') 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 { it to part1(it) }
|
||||
.onEach { println("$it") }
|
||||
.map { it.second.toLong() }
|
||||
.reduce { acc, trees -> acc * trees }
|
||||
}
|
||||
|
||||
fun main() {
|
||||
with(createDay<Day03>()) {
|
||||
println(part1().also { check(it == 294) })
|
||||
println(part2().also { check(it == 5774564250) })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user