diff --git a/src/main/kotlin/no/rodland/advent_2023/Day13.kt b/src/main/kotlin/no/rodland/advent_2023/Day13.kt index 64e05c5..cf8a9c8 100644 --- a/src/main/kotlin/no/rodland/advent_2023/Day13.kt +++ b/src/main/kotlin/no/rodland/advent_2023/Day13.kt @@ -5,23 +5,87 @@ import no.rodland.advent.Day // template generated: 13/12/2023 // Fredrik Rødland 2023 -class Day13(val input: List) : Day> { +class Day13(val input: List) : Day> { private val parsed = input.parse() - override fun partOne(): Long { - return 2 + override fun partOne(): Int { + return parsed.map { groundMap -> + val rowsHorizontal = isMirror(groundMap) + val rowsVertical = if (rowsHorizontal == 0) { + isMirror(groundMap.transpose()) + } else { + 0 + } +// debug(rowsVertical, rowsHorisontal, groundMap) + rowsVertical + 100 * rowsHorizontal + }.sum() + } + + private fun debug(rowsVertical: Int, rowsHorisontal: Int, groundMap: GroundMap) { + if (rowsVertical == 0 && rowsHorisontal == 0) { + println("no reflection for:") + } else { + println("misc v: $rowsVertical, h: $rowsHorisontal") + } + groundMap.print() + println() + } + + // 1500 too low + // 1700 too low + private fun isMirror(groundMap: GroundMap): Int { + (1.. + val map = groundMap.grounds + + val rev = map.subList(0, i).asReversed() + val forw = map.subList(i, map.size) + + val size = minOf(rev.size, forw.size) + // println("rev: $rev for: $forw") + if (rev.subList(0, size) == forw.subList(0, size)) + return i + } + return 0 } override fun partTwo(): Long { return 2 } - override fun List.parse(): List { - return map { line -> - line + override fun List.parse(): List { + val converters = joinToString("\n").split("\n\n").map { it.split("\n") } + + return converters.map { l -> + GroundMap(l) + } + + } + + data class GroundMap(val grounds: List) { + fun transpose() = GroundMap(grounds.transpose()) + val size = grounds.size + val reversed = grounds.reversed() + + fun print() { + grounds.map { line -> + println(line) + } + } } override val day = "13".toInt() } + +private fun List.transpose(): List { + val width = first().length + val height = size + return (0.. + (0.. + get(i)[j] + }.joinToString("") + } +} + + diff --git a/src/test/kotlin/no/rodland/advent_2023/Day13Test.kt b/src/test/kotlin/no/rodland/advent_2023/Day13Test.kt index fb71701..9c0d621 100644 --- a/src/test/kotlin/no/rodland/advent_2023/Day13Test.kt +++ b/src/test/kotlin/no/rodland/advent_2023/Day13Test.kt @@ -15,9 +15,9 @@ internal class Day13Test { private val data13 = "2023/input_13.txt".readFile() private val test13 = "2023/input_13_test.txt".readFile() - private val resultTestOne = 2L + private val resultTestOne = 405 private val resultTestTwo = 2L - private val resultOne = 2L + private val resultOne = 40006 private val resultTwo = 2L val test = defaultTestSuiteParseOnInit( @@ -29,6 +29,7 @@ internal class Day13Test { resultTwo, { Day13(data13) }, { Day13(test13) }, + numTestPart1 = 300 ) @Nested