1
0

Clean a bit + tests

This commit is contained in:
Hubert Van De Walle 2020-12-03 08:22:35 +01:00
parent 6ab2fbce00
commit da3628c9a7
2 changed files with 62 additions and 12 deletions

View File

@ -18,13 +18,11 @@ class Day03(@Lines val input: Input<List<String>>) {
val width = grid.first().length
while (true) {
while (y < grid.size - 1) {
x += slope.x
y += slope.y
if (x >= width) x -= width
if (y >= grid.size) break
if (grid[y][x] == '#') trees++
if (grid[y][x % width] == '#') trees++
}
return trees
@ -37,15 +35,11 @@ class Day03(@Lines val input: Input<List<String>>) {
Slope(x = 7, y = 1),
Slope(x = 1, y = 2),
)
.map { it to part1(it) }
.onEach { println("$it") }
.map { it.second.toLong() }
.map { part1(it).toLong() }
.reduce { acc, trees -> acc * trees }
}
fun main() {
with(createDay<Day03>()) {
println(part1().also { check(it == 294) })
println(part2().also { check(it == 5774564250) })
}
fun main() = with(createDay<Day03>()) {
println(part1())
println(part2())
}

View 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)
}
}
}