Skip to content

Commit

Permalink
2023 - Day 13 - part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 13, 2023
1 parent d3ea6d4 commit bb0b06f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 8 deletions.
76 changes: 70 additions & 6 deletions src/main/kotlin/no/rodland/advent_2023/Day13.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,87 @@ import no.rodland.advent.Day
// template generated: 13/12/2023
// Fredrik Rødland 2023

class Day13(val input: List<String>) : Day<Long, Long, List<String>> {
class Day13(val input: List<String>) : Day<Int, Long, List<Day13.GroundMap>> {

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..<groundMap.size).forEach { i ->
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<String>.parse(): List<String> {
return map { line ->
line
override fun List<String>.parse(): List<GroundMap> {
val converters = joinToString("\n").split("\n\n").map { it.split("\n") }

return converters.map { l ->
GroundMap(l)
}

}

data class GroundMap(val grounds: List<String>) {
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<String>.transpose(): List<String> {
val width = first().length
val height = size
return (0..<width).map { j ->
(0..<height).map { i ->
get(i)[j]
}.joinToString("")
}
}


5 changes: 3 additions & 2 deletions src/test/kotlin/no/rodland/advent_2023/Day13Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -29,6 +29,7 @@ internal class Day13Test {
resultTwo,
{ Day13(data13) },
{ Day13(test13) },
numTestPart1 = 300
)

@Nested
Expand Down

0 comments on commit bb0b06f

Please sign in to comment.