package aoc2023

import println
import readInput
import splitInts

/*
--- Day 6: Wait For It ---
https://adventofcode.com/2023/day/6
*/
fun main() {

    val inlineTestInput = """
Time:      7  15   30
Distance:  9  40  200
"""

    // Next time, don't be stupid. This is not the right formula for the problem:
    // distance = (bt * (bt+1))/2 + bt * (tt - 2 * bt)
    // Spend 30 minutes figuring out why I don't win any race when the boat is actually accelerating!
    fun part1(input: List<String>): Int {
        val times = input[0].split(":")[1].splitInts()
        val distances = input[1].split(":")[1].splitInts()
        val result = times.withIndex().map { (i, tt) ->
            IntRange(1, tt - 1).count {
                it * (tt - it) > distances[i]
            }
        }
        return result.reduce { acc, i -> acc * i }
    }

    fun part2(input: List<String>): Int {
        val tt = input[0].split(":")[1].replace(" ", "").toLong()
        val distance = input[1].split(":")[1].replace(" ", "").toLong()
        val result = LongRange(1, tt - 1).count {
            it * (tt - it) > distance
        }
        return result
    }

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

    val input = readInput("aoc2023/Day06")
    part1(input).println()
    part2(input).println()
}
