Clean a bit + tests
This commit is contained in:
parent
6ab2fbce00
commit
da3628c9a7
@ -18,13 +18,11 @@ class Day03(@Lines val input: Input<List<String>>) {
|
|||||||
|
|
||||||
val width = grid.first().length
|
val width = grid.first().length
|
||||||
|
|
||||||
while (true) {
|
while (y < grid.size - 1) {
|
||||||
x += slope.x
|
x += slope.x
|
||||||
y += slope.y
|
y += slope.y
|
||||||
|
|
||||||
if (x >= width) x -= width
|
if (grid[y][x % width] == '#') trees++
|
||||||
if (y >= grid.size) break
|
|
||||||
if (grid[y][x] == '#') trees++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return trees
|
return trees
|
||||||
@ -37,15 +35,11 @@ class Day03(@Lines val input: Input<List<String>>) {
|
|||||||
Slope(x = 7, y = 1),
|
Slope(x = 7, y = 1),
|
||||||
Slope(x = 1, y = 2),
|
Slope(x = 1, y = 2),
|
||||||
)
|
)
|
||||||
.map { it to part1(it) }
|
.map { part1(it).toLong() }
|
||||||
.onEach { println("$it") }
|
|
||||||
.map { it.second.toLong() }
|
|
||||||
.reduce { acc, trees -> acc * trees }
|
.reduce { acc, trees -> acc * trees }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun main() {
|
fun main() = with(createDay<Day03>()) {
|
||||||
with(createDay<Day03>()) {
|
println(part1())
|
||||||
println(part1().also { check(it == 294) })
|
println(part2())
|
||||||
println(part2().also { check(it == 5774564250) })
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
56
days/src/test/kotlin/Day03Test.kt
Normal file
56
days/src/test/kotlin/Day03Test.kt
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package be.vandewalleh.aoc.days
|
||||||
|
|
||||||
|
import be.vandewalleh.aoc.utils.input.Input
|
||||||
|
import be.vandewalleh.aoc.utils.input.createDay
|
||||||
|
import org.assertj.core.api.Assertions.assertThat
|
||||||
|
import org.junit.jupiter.api.Nested
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
|
class Day03Test {
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
inner class Example {
|
||||||
|
private val example = listOf(
|
||||||
|
"..##.......",
|
||||||
|
"#...#...#..",
|
||||||
|
".#....#..#.",
|
||||||
|
"..#.#...#.#",
|
||||||
|
".#...##..#.",
|
||||||
|
"..#.##.....",
|
||||||
|
".#.#.#....#",
|
||||||
|
".#........#",
|
||||||
|
"#.##...#...",
|
||||||
|
"#...##....#",
|
||||||
|
".#..#...#.#",
|
||||||
|
).let { Input(it) }
|
||||||
|
|
||||||
|
private val day03 = Day03(example)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `part1 result`() {
|
||||||
|
assertThat(day03.part1()).isEqualTo(7)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `part2 result`() {
|
||||||
|
assertThat(day03.part2()).isEqualTo(336)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
inner class RealInput {
|
||||||
|
private val day03 = createDay<Day03>()
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `part1 result`() {
|
||||||
|
assertThat(day03.part1()).isEqualTo(294)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `part2 result`() {
|
||||||
|
assertThat(day03.part2()).isEqualTo(5774564250)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user