From da3628c9a73fa36c9d1e3160ce983a913c9507e8 Mon Sep 17 00:00:00 2001 From: Hubert Van De Walle Date: Thu, 3 Dec 2020 08:22:35 +0100 Subject: [PATCH] Clean a bit + tests --- days/src/main/kotlin/Day03.kt | 18 ++++------ days/src/test/kotlin/Day03Test.kt | 56 +++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 12 deletions(-) create mode 100644 days/src/test/kotlin/Day03Test.kt diff --git a/days/src/main/kotlin/Day03.kt b/days/src/main/kotlin/Day03.kt index aad40ea..4b7f327 100644 --- a/days/src/main/kotlin/Day03.kt +++ b/days/src/main/kotlin/Day03.kt @@ -18,13 +18,11 @@ class Day03(@Lines val input: Input>) { 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>) { 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()) { - println(part1().also { check(it == 294) }) - println(part2().also { check(it == 5774564250) }) - } +fun main() = with(createDay()) { + println(part1()) + println(part2()) } diff --git a/days/src/test/kotlin/Day03Test.kt b/days/src/test/kotlin/Day03Test.kt new file mode 100644 index 0000000..e812f9f --- /dev/null +++ b/days/src/test/kotlin/Day03Test.kt @@ -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() + + @Test + fun `part1 result`() { + assertThat(day03.part1()).isEqualTo(294) + } + + @Test + fun `part2 result`() { + assertThat(day03.part2()).isEqualTo(5774564250) + } + } + +}