package aoc2024

import CharGrid
import RelPos
import println
import readInput

/*
--- Day 4: Ceres Search ---
https://adventofcode.com/2024/day/4
*/
fun main() {

    val inlineTestInput = """
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
"""

    fun part1(input: List<String>): Int {
        val grid = CharGrid(input)
        val relposes = listOf(
            listOf(RelPos(0, 0), RelPos(1, 0), RelPos(2, 0), RelPos(3, 0)),
            listOf(RelPos(0, 0), RelPos(-1, 0), RelPos(-2, 0), RelPos(-3, 0)),
            listOf(RelPos(0, 0), RelPos(1, 1), RelPos(2, 2), RelPos(3, 3)),
            listOf(RelPos(0, 0), RelPos(-1, -1), RelPos(-2, -2), RelPos(-3, -3)),
            listOf(RelPos(0, 0), RelPos(-1, 1), RelPos(-2, 2), RelPos(-3, 3)),
            listOf(RelPos(0, 0), RelPos(1, -1), RelPos(2, -2), RelPos(3, -3)),
            listOf(RelPos(0, 0), RelPos(0, 1), RelPos(0, 2), RelPos(0, 3)),
            listOf(RelPos(0, 0), RelPos(0, -1), RelPos(0, -2), RelPos(0, -3)),
        )
        return grid.countStrings(relposes, "XMAS")
    }

    fun part2(input: List<String>): Int {
        val grid = CharGrid(input)
        val relposes = listOf(
            listOf(RelPos(-1, -1), RelPos(0, 0), RelPos(1, 1)),
            listOf(RelPos(-1, 1), RelPos(0, 0), RelPos(1, -1)),
        )
        return grid.findCombinedMatches(relposes) { it.all { it == "MAS" || it == "SAM" } }.count()
    }

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

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