-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day23.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
kt/src/commonTest/kotlin/com/github/ephemient/aoc2022/Day23Test.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
....#.. | ||
..###.# | ||
#...#.# | ||
.#...## | ||
#.###.. | ||
##.#.## | ||
.#..#.. |