r/backtickbot • u/backtickbot • Dec 03 '20
https://np.reddit.com/r/adventofcode/comments/k5qsrk/2020_day_03_solutions/gegnm82/
Kotlin
object Forest {
/** * Forest is organized as a 2D array where, Y -> 1st dim, X -> 2nd dim. * Each spot can be analyzed with [isOccupied] to see if there is a tree there. */ fun parse(data: String): List<List<Char>> = data.split("\n") .reversed() .map { it.toCharArray().toList() } fun walkAndCount(forest: List<List<Char>>, stepX: Int, stepY: Int) = walkAndCountRecursively( currentTreeCount = 0, currentX = 0, currentY = forest.size -1, stepX, stepY, forest ) private fun walkAndCountRecursively( currentTreeCount: Int, currentX: Int, currentY: Int, stepX: Int, stepY: Int, forest: List<List<Char>> ): Int { val nextGenForest = expandForestIfRequired(currentX, forest) val currentSpot = nextGenForest[currentY][currentX] val nextTreeCount = currentTreeCount + if (currentSpot.isOccupied()) 1 else 0 val nextX = currentX + stepX val nextY = currentY - stepY if (nextY < 0) { // we have passed through the end/bottom of the forest return nextTreeCount } return walkAndCountRecursively( currentTreeCount = nextTreeCount, currentX = nextX, currentY = nextY, stepX, stepY, nextGenForest ) } private fun Char.isOccupied() = this == '#' /** * Expand the forest, by duplicating it along the X-axis (the 2nd dim) for each Y (row), if we are out of the * currently known bounds of the forest (along the X-axis). */ private fun expandForestIfRequired(currentX: Int, forest: List<List<Char>>) = if (currentX < forest[0].size) { forest } else { forest.map { it + it } }}
internal class ForestTest {
private val testInput = """ ..##....... #...#...#.. .#....#..#. ..#.#...#.# .#...##..#. ..#.##..... .#.#.#....# .#........# #.##...#... #...##....# .#..#...#.# """.trimIndent() @Test fun `part 1 test`() { val forest = Forest.parse(testInput) Assertions.assertEquals(7, Forest.walkAndCount(forest, stepX = 3, stepY = 1)) } @Test fun `part 1 real`() { val data = File("src/test/resources/aoc2020/day3/input.txt").readText() val forest = Forest.parse(data) Assertions.assertEquals(209, Forest.walkAndCount(forest, stepX = 3, stepY = 1)) } @Test fun `part 2 test`() { val forest = Forest.parse(testInput) Assertions.assertEquals(336, walkMultipleSlopesAndAccumulateTreeCount(forest)) } @Test fun `part 2 real`() { val data = File("src/test/resources/aoc2020/day3/input.txt").readText() val forest = Forest.parse(data) Assertions.assertEquals(1574890240, walkMultipleSlopesAndAccumulateTreeCount(forest)) } private fun walkMultipleSlopesAndAccumulateTreeCount(forest: List<List<Char>>) = listOf( Pair(1, 1), Pair(3, 1), Pair(5, 1), Pair(7, 1), Pair(1, 2), ).fold(1) { acc, (stepX, stepY) -> acc * Forest.walkAndCount(forest, stepX, stepY) }}
1
Upvotes