package aoc2022

import println
import readInput

/*
--- Day 4: Camp Cleanup ---
https://adventofcode.com/2022/day/4
*/
fun main() {

    val inlineTestInput = """
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8
"""

    fun part1(input: List<String>): Int {
        return input.map {
            val (r1, r2) = it.split(",").map {
                val (a, b) = it.split("-").map { it.toInt() }
                a..b
            }
            if ((r1.start >= r2.start && r1.endInclusive <= r2.endInclusive) ||
                (r2.start >= r1.start && r2.endInclusive <= r1.endInclusive)
            ) 1 else 0
        }.sum()
    }

    fun part2(input: List<String>): Int {
        return input.map {
            val (r1, r2) = it.split(",").map {
                val (a, b) = it.split("-").map { it.toInt() }
                a..b
            }
            if (r1.start in r2 || r1.endInclusive in r2 ||
                r2.start in r1 || r2.endInclusive in r1
            ) 1 else 0
        }.sum()
    }

    // test if implementation meets criteria from the description, like:
    val testInput = inlineTestInput.trim().reader().readLines()
    //val testInput = readInput("aoc2022/Day04_test")
    println("Part 1 Test: " + part1(testInput))
    println("Part 2 Test: " + part2(testInput))
    check(part1(testInput) == 2)
    check(part2(testInput) == 4)

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