Generify solution
This commit is contained in:
parent
a0149c26e0
commit
264ba1cb91
@ -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<List<String>>) {
|
||||
|
||||
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 <T> parseGrid(pointFactory: (x: Int, y: Int) -> T): MutableMap<T, State> {
|
||||
val grid = mutableMapOf<T, State>()
|
||||
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 <T> step(grid: MutableMap<T, State>, neighbours: (T) -> List<T>) {
|
||||
val modifications = mutableMapOf<T, State>()
|
||||
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<Point> {
|
||||
val points = mutableListOf<Point>()
|
||||
for (x in point.x - 1..point.x + 1) {
|
||||
@ -46,82 +86,9 @@ class Day17(@Lines val input: Input<List<String>>) {
|
||||
return points
|
||||
}
|
||||
|
||||
fun part1(): Int {
|
||||
val grid = parseGrid3()
|
||||
repeat(6) { step3(grid) }
|
||||
return grid.values.count { it == State.Active }
|
||||
}
|
||||
|
||||
private fun parseGrid3(): MutableMap<Point, State> {
|
||||
val grid = mutableMapOf<Point, State>()
|
||||
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<Point4, State> {
|
||||
val grid = mutableMapOf<Point4, State>()
|
||||
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<Point, State>) {
|
||||
val pointsToConsider = grid.keys.flatMap { neighbours3(it) }.toSet()
|
||||
|
||||
val modifications = mutableMapOf<Point, State>()
|
||||
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<Point4, State>) {
|
||||
val pointsToConsider = grid.keys.flatMap { neighbours4(it) }.toSet()
|
||||
|
||||
val modifications = mutableMapOf<Point4, State>()
|
||||
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<Day17>(
|
||||
)) {
|
||||
fun main() = with(createDay<Day17>()) {
|
||||
println(part1())
|
||||
println(part2())
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user