This commit is contained in:
Chris Hodges 2025-12-04 06:41:19 +01:00
parent 1f323bcc82
commit 732c69df0e
2 changed files with 60 additions and 1 deletions

View File

@ -152,6 +152,9 @@ class CharGrid {
fun applyWithPos(op: (grid: CharGrid, pos: RelPos) -> Char?) =
generateGridPos().forEach { this[it] = op(this, it) }
fun applyWithPos(relposes: Iterable<RelPos>, op: (grid: CharGrid, pos: RelPos) -> Char?) =
relposes.forEach { this[it] = op(this, it) }
fun apply(op: (content: Char) -> Char?) =
applyWithPos { grid: CharGrid, pos -> op(grid[pos]) }
@ -201,7 +204,7 @@ class CharGrid {
fun matchRelative(c: Int, r: Int, relposes: Iterable<RelPos>, predicate: (char: Char) -> Boolean): List<RelPos> =
relposes.filter { predicate(get(c + it.dc, r + it.dr)) }
fun matchRelativeRelPos(pos: RelPos, relposes: Iterable<RelPos>, predicate: (char: Char) -> Boolean): List<RelPos> =
fun matchRelative(pos: RelPos, relposes: Iterable<RelPos>, predicate: (char: Char) -> Boolean): List<RelPos> =
relposes.filter { predicate(get(pos.translate(it))) }
fun matchAbsoluteRelPos(pos: RelPos, relposes: Iterable<RelPos>, predicate: (char: Char) -> Boolean): List<RelPos> =

56
src/aoc2025/Day04.kt Normal file
View File

@ -0,0 +1,56 @@
package aoc2025
import CharGrid
import println
import readInput
/*
--- Day 4: Printing Department ---
https://adventofcode.com/2025/day/4
*/
fun main() {
val inlineTestInput = """
..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@.
"""
fun part1(input: List<String>): Int {
val grid = CharGrid(input)
val matches = grid.findMatchesRelPos { it == '@' }.filter { grid.matchRelative(it, CharGrid.BOX_POS) { it == '@' }.size < 4 }
return matches.count()
}
fun part2(input: List<String>): Int {
val grid = CharGrid(input)
var removed = 0
do {
val matches = grid.findMatchesRelPos { it == '@' }.filter { grid.matchRelative(it, CharGrid.BOX_POS) { it == '@' }.size < 4 }
grid.applyWithPos(matches) { _, _ -> 'x' }
removed += matches.size
} while (matches.isNotEmpty())
return removed
}
// test if implementation meets criteria from the description, like:
val testInput = inlineTestInput.trim().reader().readLines()
//val testInput = readInput("aoc2025/Day04_test")
val testInputPart1Result = part1(testInput)
println("Part 1 Test: $testInputPart1Result")
val testInputPart2Result = part2(testInput)
println("Part 2 Test: $testInputPart2Result")
check(testInputPart1Result == 13)
check(testInputPart2Result == 43)
val input = readInput("aoc2025/Day04")
part1(input).println()
part2(input).println()
}