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()
}
