From 264ba1cb918985ec24a81384e23c4625dbe55e81 Mon Sep 17 00:00:00 2001 From: Hubert Van De Walle Date: Thu, 17 Dec 2020 07:24:30 +0100 Subject: [PATCH] Generify solution --- days/src/main/kotlin/Day17.kt | 117 ++++++++++++---------------------- 1 file changed, 42 insertions(+), 75 deletions(-) diff --git a/days/src/main/kotlin/Day17.kt b/days/src/main/kotlin/Day17.kt index e14c573..62c83de 100644 --- a/days/src/main/kotlin/Day17.kt +++ b/days/src/main/kotlin/Day17.kt @@ -6,7 +6,6 @@ import be.vandewalleh.aoc.utils.input.Lines import be.vandewalleh.aoc.utils.input.createDay data class Point(val x: Int, val y: Int, val z: Int) - data class Point4(val x: Int, val y: Int, val z: Int, val blah: Int) enum class State { Active, Inactive } @@ -14,6 +13,47 @@ enum class State { Active, Inactive } @Day(17) class Day17(@Lines val input: Input>) { + fun part1(): Int { + val grid = parseGrid { x, y -> Point(x, y, 0) } + repeat(6) { step(grid, ::neighbours3) } + return grid.values.count { it == State.Active } + } + + fun part2(): Int { + val grid = parseGrid { x, y -> Point4(x, y, 0, 0) } + repeat(6) { step(grid, ::neighbours4) } + return grid.values.count { it == State.Active } + } + + private fun parseGrid(pointFactory: (x: Int, y: Int) -> T): MutableMap { + val grid = mutableMapOf() + input.value.forEachIndexed { index, row -> + row.forEachIndexed { col, char -> + val state = if (char == '#') State.Active else State.Inactive + grid[pointFactory(index, col)] = state + } + } + return grid + } + + private fun step(grid: MutableMap, neighbours: (T) -> List) { + val modifications = mutableMapOf() + val pointsToConsider = grid.keys.flatMap { neighbours(it) }.toSet() + for (point in pointsToConsider) { + val neighbours = neighbours(point) + val activeNeighboursCount = neighbours.count { grid[it] ?: State.Inactive == State.Active } + val state = grid[point] ?: State.Inactive + if (state == State.Active && activeNeighboursCount !in 2..3) { + modifications[point] = State.Inactive + } else if (activeNeighboursCount == 3) { + modifications[point] = State.Active + } + } + for ((point, state) in modifications) { + grid[point] = state + } + } + private fun neighbours3(point: Point): List { val points = mutableListOf() for (x in point.x - 1..point.x + 1) { @@ -46,82 +86,9 @@ class Day17(@Lines val input: Input>) { return points } - fun part1(): Int { - val grid = parseGrid3() - repeat(6) { step3(grid) } - return grid.values.count { it == State.Active } - } - - private fun parseGrid3(): MutableMap { - val grid = mutableMapOf() - input.value.forEachIndexed { index, row -> - row.forEachIndexed { col, char -> - val state = if (char == '#') State.Active else State.Inactive - grid[Point(index, col, 0)] = state - } - } - return grid - } - - private fun parseGrid4(): MutableMap { - val grid = mutableMapOf() - input.value.forEachIndexed { index, row -> - row.forEachIndexed { col, char -> - val state = if (char == '#') State.Active else State.Inactive - grid[Point4(index, col, 0, 0)] = state - } - } - return grid - } - - private fun step3(grid: MutableMap) { - val pointsToConsider = grid.keys.flatMap { neighbours3(it) }.toSet() - - val modifications = mutableMapOf() - for (point in pointsToConsider) { - val neighbours = neighbours3(point) - val activeNeighboursCount = neighbours.count { grid[it] ?: State.Inactive == State.Active } - val state = grid[point] ?: State.Inactive - if (state == State.Active && activeNeighboursCount !in 2..3) { - modifications[point] = State.Inactive - } else if (activeNeighboursCount == 3) { - modifications[point] = State.Active - } - } - for ((point, state) in modifications) { - grid[point] = state - } - } - - private fun step4(grid: MutableMap) { - val pointsToConsider = grid.keys.flatMap { neighbours4(it) }.toSet() - - val modifications = mutableMapOf() - for (point in pointsToConsider) { - val neighbours = neighbours4(point) - val activeNeighboursCount = neighbours.count { grid[it] ?: State.Inactive == State.Active } - val state = grid[point] ?: State.Inactive - if (state == State.Active && activeNeighboursCount !in 2..3) { - modifications[point] = State.Inactive - } else if (activeNeighboursCount == 3) { - modifications[point] = State.Active - } - } - for ((point, state) in modifications) { - grid[point] = state - } - } - - fun part2(): Int { - val grid = parseGrid4() - repeat(6) { step4(grid) } - return grid.values.count { it == State.Active } - } - } -fun main() = with(createDay( -)) { +fun main() = with(createDay()) { println(part1()) println(part2()) }