Skip to content

Commit

Permalink
Day 23: Unstable Diffusion
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 23, 2022
1 parent f3cfbcd commit 3324746
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ Development occurs in language-specific directories:
|[Day20.hs](hs/src/Day20.hs)|[Day20.kt](kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day20.kt)|[day20.py](py/aoc2022/day20.py)|[day20.rs](rs/src/day20.rs)|
|[Day21.hs](hs/src/Day21.hs)|[Day21.kt](kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day21.kt)|[day21.py](py/aoc2022/day21.py)|[day21.rs](rs/src/day21.rs)|
|[Day22.hs](hs/src/Day22.hs)|[Day22.kt](kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day22.kt)|[day22.py](py/aoc2022/day22.py)|[day22.rs](rs/src/day22.rs)|
|[Day23.hs](hs/src/Day23.hs)|
|[Day23.hs](hs/src/Day23.hs)|[Day23.kt](kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day23.kt)|
76 changes: 76 additions & 0 deletions kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day23.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.github.ephemient.aoc2022

@Day
class Day23(lines: List<String>) {
private val states = directions.scan(
buildSet { lines.forEachIndexed { y, line -> line.forEachIndexed { x, c -> if (c == '#') add(x to y) } } }
) { state, directions ->
val proposals = state.groupingBy { position ->
if (position.neighbors().none { it in state }) {
position
} else {
directions.firstNotNullOfOrNull { direction ->
if (position.neighbors(direction).none { it in state }) position.move(direction) else null
} ?: position
}
}.aggregateTo(mutableMapOf()) { _, _: IntPair?, value, first -> if (first) value else null }
proposals.entries.removeAll { (key, value) -> value == null || key == value }
buildSet(state.size) {
addAll(state)
removeAll(proposals.values.toSet())
addAll(proposals.keys)
}
}

@Day.Part
fun part1(): Int {
val state = states.elementAt(10)
val minX = state.minOf { it.first }
val maxX = state.maxOf { it.first }
val minY = state.minOf { it.second }
val maxY = state.maxOf { it.second }
return (maxX - minX + 1) * (maxY - minY + 1) - state.size
}

@Day.Part
fun part2(): Int = states.zipWithNext().indexOfFirst { (prev, cur) -> prev == cur } + 1

private enum class Direction {
N, S, W, E
}

companion object {
private val directions = sequence {
val directions = ArrayDeque(Direction.values().asList())
while (true) {
yield(directions.toList())
directions.add(directions.removeFirst())
}
}

private fun IntPair.neighbors(): Array<IntPair> = arrayOf(
first + 1 to second,
first + 1 to second + 1,
first to second + 1,
first - 1 to second + 1,
first - 1 to second,
first - 1 to second - 1,
first to second - 1,
first + 1 to second - 1,
)

private fun IntPair.neighbors(direction: Direction): Array<IntPair> = when (direction) {
Direction.N -> arrayOf(first - 1 to second - 1, first to second - 1, first + 1 to second - 1)
Direction.S -> arrayOf(first - 1 to second + 1, first to second + 1, first + 1 to second + 1)
Direction.W -> arrayOf(first - 1 to second - 1, first - 1 to second, first - 1 to second + 1)
Direction.E -> arrayOf(first + 1 to second - 1, first + 1 to second, first + 1 to second + 1)
}

private fun IntPair.move(direction: Direction) = when (direction) {
Direction.N -> first to second - 1
Direction.S -> first to second + 1
Direction.W -> first - 1 to second
Direction.E -> first + 1 to second
}
}
}
16 changes: 16 additions & 0 deletions kt/src/commonTest/kotlin/com/github/ephemient/aoc2022/Day23Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.ephemient.aoc2022

import kotlin.test.Test
import kotlin.test.assertEquals

class Day23Test {
@Test
fun part1() {
assertEquals(110, Day23(getTestInput(23)).part1())
}

@Test
fun part2() {
assertEquals(20, Day23(getTestInput(23)).part2())
}
}
7 changes: 7 additions & 0 deletions kt/src/jvmTest/resources/day23.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
....#..
..###.#
#...#.#
.#...##
#.###..
##.#.##
.#..#..

0 comments on commit 3324746

Please sign in to comment.