package aoc2024

import CharGrid
import println
import readInput

/*
--- Day 25: Code Chronicle ---
https://adventofcode.com/2024/day/25
*/
fun main() {

    val inlineTestInput = """
#####
.####
.####
.####
.#.#.
.#...
.....

#####
##.##
.#.##
...##
...#.
...#.
.....

.....
#....
#....
#...#
#.#.#
#.###
#####

.....
.....
#.#..
###..
###.#
###.#
#####

.....
.....
.....
#....
#.#..
#.#.#
#####
"""

    fun part1(input: List<String>): Int {
        var p = 0
        val topRow = IntRange(0, 4)
        val locks = ArrayList<IntArray>()
        val keys = ArrayList<IntArray>()
        while (p < input.size) {
            val grid = CharGrid(input.subList(p, p + 7))
            if (topRow.all { grid[it, 0] == '#' }) {
                val lock = topRow.map { rc -> (topRow.find { grid[rc, it + 1] != '#' } ?: 5) }.toIntArray()
                locks.add(lock)
            } else {
                val key = topRow.map { rc -> 5 - (topRow.find { grid[rc, 5 - it] != '#' } ?: 5) }.toIntArray()
                keys.add(key)
            }
            p += 8
        }
        return locks.sumOf { lock -> keys.count { key -> key.zip(lock).all { it.first >= it.second } } }
    }

    fun part2(input: List<String>): Int {
        return 0
    }

    // test if implementation meets criteria from the description, like:
    val testInput = inlineTestInput.trim().reader().readLines()
    //val testInput = readInput("aoc2024/Day25_test")
    val testInputPart1Result = part1(testInput)
    println("Part 1 Test: $testInputPart1Result")
    val testInputPart2Result = part2(testInput)
    println("Part 2 Test: $testInputPart2Result")
    check(testInputPart1Result == 3)
    check(testInputPart2Result == 0)

    val input = readInput("aoc2024/Day25")
    part1(input).println()
    part2(input).println()
}
