

Kotlin
I reached my solution with no aoc spoilers, after some research I figured out, that there really is no easy solution to this “packing problem”. So with a sigh, I wrote a brute-force approach which tries every shape in every possible orientation, with a simple check if they could even fit at all first. To make it a little fun, I decided to implement my own matrix rotation function. I made it generic and prettier a few days later, but this code still uses the old one, due to it specifically using a BooleanArray for a (hopefully) smaller memory / performance footprint. As all shapes are 3x3, the brute-force algorithm works by placing the center position everywhere except for the outer edges of the available space.
Now that I have read the answers here, I feel stupid… But unlike all of your solutions here, mine do indeed work on the example and real input, so take that I guess. In hindsight, I should have been a lot more suspicious of how this is able to run in under 10 seconds, but oh well.
Obviously very bloated code due to it technically working on non trivial cases:
Solution
private fun rotate3by3(grid: Array<BooleanArray>, k: Int) {
if (k % 4 == 0) return
val indices = listOf(
0 to 0,
0 to 1,
0 to 2,
1 to 2,
2 to 2,
2 to 1,
2 to 0,
1 to 0,
)
val ring = indices.map { (x, y) -> grid[x][y] }.toMutableList()
Collections.rotate(ring, 2 * (k % 4))
indices.forEachIndexed { index, (x, y) -> grid[x][y] = ring[index] }
}
class Day12 : Puzzle {
lateinit var shapes: List<Shape>
lateinit var areas: List<Area>
private val points3by3 = listOf(
-1 to -1,
-1 to 0,
-1 to 1,
0 to -1,
0 to 0,
0 to 1,
1 to -1,
1 to 0,
1 to 1,
)
override fun readFile() {
val input = readInputFromFile(2025, 12, false)
val split = input.trim().split("\n\n")
val shapeList = mutableListOf<Shape>()
for (shape in split.take(split.size - 1)) {
shapeList.add(Shape(shape))
}
shapes = shapeList
areas = split.last().lines().map { Area(it) }
}
override fun solvePartOne(): String {
var sum = 0L
for (area in areas) {
val shapesToFit = area.idCount.asSequence()
.filter { it.value > 0 }
.flatMap { entry -> List(entry.value) { shapes[entry.key] } }
.sortedByDescending { it.size }
// trivial case
if (shapesToFit.sumOf { it.size } > area.size) continue
sum += if (pack(FilledArea(area), shapesToFit)) 1L else 0L
}
return sum.toString()
}
private fun pack(filledArea: FilledArea, shapes: Sequence<Shape>): Boolean {
val next = shapes.firstOrNull() ?: return true
for (state in next.states()) {
for (centerPos in filledArea.fits(state)) {
if (pack(filledArea.newAreaWithShape(centerPos, state), shapes.drop(1))) return true
}
}
return false
}
override fun solvePartTwo(): String {
return "Not implemented"
}
data class Shape(val inputString: String) {
val index: Int = inputString.lines()[0].replace(":", "").toInt()
val grid: Array<BooleanArray>
val size
get() = grid.sumOf { row -> row.count { it } }
init {
val tempGrid = mutableListOf<BooleanArray>()
for ((i, line) in inputString.lines().withIndex()) {
if (i == 0) continue // skip index
val list = mutableListOf<Boolean>()
for (char in line) {
when (char) {
'#' -> list.add(true)
'.' -> list.add(false)
else -> throw Exception("Malformed input line: $line")
}
}
tempGrid.add(list.toBooleanArray())
}
grid = tempGrid.toTypedArray()
}
// generate all rotations + flips
fun states(): List<Array<BooleanArray>> {
val out = mutableListOf<Array<BooleanArray>>()
val flips = mutableListOf(
grid.map { it.copyOf() }.toTypedArray(), // normal
grid.map { it.copyOf() }.reversed().toTypedArray(), // horizontal
grid.map { it.copyOf().reversed().toBooleanArray() }.toTypedArray(), // vertical
grid.map { it.copyOf().reversed().toBooleanArray() }.reversed().toTypedArray(), // both
)
for (flip in flips) {
for (k in 0..3) {
val arr = flip.map { it.copyOf() }.toTypedArray()
rotate3by3(arr, k)
out.add(arr)
}
}
return out.distinctBy { it.contentDeepHashCode() }
}
}
data class Area(val inputString: String) {
val width: Int
val height: Int
val idCount: Map<Int, Int> // <index, count>
val size
get() = width * height
init {
val split = inputString.split(": ")
val sizeSplit = split[0].split("x")
width = sizeSplit[0].toInt()
height = sizeSplit[1].toInt()
idCount = split[1].split(" ").mapIndexed { index, s -> index to s.toInt() }
.filter { it.second != 0 }
.toMap()
}
}
inner class FilledArea(
val width: Int,
val height: Int,
val grid: Array<BooleanArray> = Array(height) { BooleanArray(width) { false } },
) {
constructor(area: Area) : this(area.width, area.height)
// 3x3 shape -> position = center point
// returns every possible center point
fun fits(shape: Array<BooleanArray>): List<Pair<Int, Int>> {
val out = mutableListOf<Pair<Int, Int>>()
// 3x3 centers -> skip edges
for (r in grid.indices.filter { it != 0 && it != grid.size - 1 }) {
for (c in grid[r].indices.filter { it != 0 && it != grid[r].size - 1 }) {
val fits = points3by3.count { (dx, dy) ->
grid[r + dx][c + dy] && shape[1 + dx][1 + dy]
} == 0
if (fits) out.add(r to c)
}
}
return out
}
// returns a new FilledArea, with the given shape inserted into it
// no space check !!
fun newAreaWithShape(centerPos: Pair<Int, Int>, shape: Array<BooleanArray>): FilledArea {
val newGrid = grid.map { it.copyOf() }.toTypedArray()
val (cx, cy) = centerPos
points3by3.forEach { (dx, dy) ->
newGrid[cx + dx][cy + dy] = shape[1 + dx][1 + dy]
}
return FilledArea(width, height, newGrid)
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as FilledArea
if (width != other.width) return false
if (height != other.height) return false
if (!grid.contentDeepEquals(other.grid)) return false
return true
}
override fun hashCode(): Int {
var result = width
result = 31 * result + height
result = 31 * result + grid.contentDeepHashCode()
return result
}
}
}
full code on Codeberg



















Thanks! Yeah over the year I got a few more puzzles done, and I figured I wanted to start into this year’s aoc with all previous ones completed.