Skip to content

Commit

Permalink
2023 - Day 13 - part 2 - with help from ephemient - my logic didn't w…
Browse files Browse the repository at this point in the history
…ork...
  • Loading branch information
fmmr committed Dec 13, 2023
1 parent bb0b06f commit 7e1e9f7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 33 deletions.
72 changes: 42 additions & 30 deletions src/main/kotlin/no/rodland/advent_2023/Day13.kt
Original file line number Diff line number Diff line change
@@ -1,65 +1,77 @@
package no.rodland.advent_2023

import no.rodland.advent.Day
import java.lang.Integer.min

// template generated: 13/12/2023
// Fredrik Rødland 2023

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

private val parsed = input.parse()

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()
return run(::partOneCompare)
}

override fun partTwo(): Int {
return run(::partTwoCompare)
}

private fun debug(rowsVertical: Int, rowsHorisontal: Int, groundMap: GroundMap) {
if (rowsVertical == 0 && rowsHorisontal == 0) {
println("no reflection for:")
private fun run(compare: (List<String>, List<String>) -> Boolean) = parsed.sumOf { groundMap ->
val rowsHorizontal = isMirror(groundMap, compare)
val rowsVertical = if (rowsHorizontal == 0) {
isMirror(groundMap.transpose(), compare)
} else {
println("misc v: $rowsVertical, h: $rowsHorisontal")
0
}
groundMap.print()
println()
rowsVertical + 100 * rowsHorizontal
}

// 1500 too low
// 1700 too low
private fun isMirror(groundMap: GroundMap): Int {
private fun isMirror(groundMap: GroundMap, compare: (List<String>, List<String>) -> Boolean): 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
val b = compare(rev, forw)
if (b) return i
}
return 0
}

override fun partTwo(): Long {
return 2
private fun partOneCompare(reverseList: List<String>, forwardList: List<String>): Boolean {
val size = min(reverseList.size, forwardList.size)
return reverseList.subList(0, size) == forwardList.subList(0, size)
}

override fun List<String>.parse(): List<GroundMap> {
val converters = joinToString("\n").split("\n\n").map { it.split("\n") }

return converters.map { l ->
GroundMap(l)
// https://github.com/ephemient/aoc2023/blob/main/kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day13.kt
private fun partTwoCompare(reverseList: List<String>, forwardList: List<String>): Boolean {
val size = min(reverseList.size, forwardList.size)
var almostEqual = false

// hm - this didn't work for some reason - so...
// var diffCount = 0
// for (i in 0..<size) {
// if (reverseList[i] != forwardList[i]) {
// diffCount++
// }
// }
// ..."borrowing" the followign code from ephemient
for (i in 0..<size) {
val a = reverseList[i]
val b = forwardList[i]
val delta = a.indices.count { a[it] != b[it] }
if (delta > 1) return false
if (delta == 1) if (almostEqual) return false else almostEqual = true
}
return almostEqual
}

override fun List<String>.parse(): List<GroundMap> {
return joinToString("\n").split("\n\n").map { it.split("\n") }.map { l -> GroundMap(l) }
}

data class GroundMap(val grounds: List<String>) {
Expand Down
7 changes: 4 additions & 3 deletions src/test/kotlin/no/rodland/advent_2023/Day13Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ internal class Day13Test {
private val test13 = "2023/input_13_test.txt".readFile()

private val resultTestOne = 405
private val resultTestTwo = 2L
private val resultTestTwo = 400
private val resultOne = 40006
private val resultTwo = 2L
private val resultTwo = 28627

val test = defaultTestSuiteParseOnInit(
Day13(data13),
Expand All @@ -29,7 +29,8 @@ internal class Day13Test {
resultTwo,
{ Day13(data13) },
{ Day13(test13) },
numTestPart1 = 300
numTestPart1 = 300,
numTestPart2 = 300
)

@Nested
Expand Down

0 comments on commit 7e1e9f7

Please sign in to comment.