1
0
This commit is contained in:
Hubert Van De Walle 2020-12-17 06:34:31 +01:00
parent fecc1df668
commit a0149c26e0
2 changed files with 135 additions and 0 deletions

View File

@ -0,0 +1,127 @@
package be.vandewalleh.aoc.days
import be.vandewalleh.aoc.utils.input.Day
import be.vandewalleh.aoc.utils.input.Input
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 }
@Day(17)
class Day17(@Lines val input: Input<List<String>>) {
private fun neighbours3(point: Point): List<Point> {
val points = mutableListOf<Point>()
for (x in point.x - 1..point.x + 1) {
for (y in point.y - 1..point.y + 1) {
for (z in point.z - 1..point.z + 1) {
val generatedPoint = Point(x, y, z)
if (generatedPoint != point)
points.add(generatedPoint)
}
}
}
check(points.size == 26) { "Points size was ${points.size}" }
return points
}
private fun neighbours4(point: Point4): List<Point4> {
val points = mutableListOf<Point4>()
for (x in point.x - 1..point.x + 1) {
for (y in point.y - 1..point.y + 1) {
for (z in point.z - 1..point.z + 1) {
for (blah in point.blah - 1..point.blah + 1) {
val generatedPoint = Point4(x, y, z, blah)
if (generatedPoint != point)
points.add(generatedPoint)
}
}
}
}
check(points.size == 80) { "Points size was ${points.size}" }
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>(
)) {
println(part1())
println(part2())
}

View File

@ -0,0 +1,8 @@
##..#.#.
#####.##
#######.
#..#..#.
#.#...##
..#....#
....#..#
..##.#..