Skip to content

Commit

Permalink
day 13
Browse files Browse the repository at this point in the history
  • Loading branch information
norganos committed Dec 13, 2023
1 parent d7d3383 commit 113b355
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 5 deletions.
82 changes: 81 additions & 1 deletion src/main/kotlin/de/linkel/aoc/Day13.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,92 @@ package de.linkel.aoc
import de.linkel.aoc.base.AbstractLinesAdventDay
import de.linkel.aoc.base.QuizPart
import jakarta.inject.Singleton
import kotlin.math.min

@Singleton
class Day13: AbstractLinesAdventDay<Int>() {
override val day = 13

override fun process(part: QuizPart, lines: Sequence<String>): Int {
return 0
return lines.split { it.isEmpty() }
.sumOf { rows ->
val cols = (0 until rows.first().length)
.map { c ->
rows.map { it[c] }.joinToString("")
}
val mirroredRows = findReflection(rows)
val mirroredCols = findReflection(cols)

val aResult = mirroredCols + mirroredRows * 100
if (part == QuizPart.A) {
aResult
} else {
val mrows = rows.toMutableList()
val mcols = cols.toMutableList()
coordinates(rows.size, cols.size)
.firstNotNullOf { pos ->
val backup = mrows[pos.first][pos.second]
val char = if (backup == '#') '.' else '#'
mrows[pos.first] = mrows[pos.first].replaceIndex(pos.second, char)
mcols[pos.second] = mcols[pos.second].replaceIndex(pos.first, char)

val mirroredRows2 = findReflection(mrows, mirroredRows)
val mirroredCols2 = findReflection(mcols, mirroredCols)
val result = mirroredCols2 + mirroredRows2 * 100

mrows[pos.first] = mrows[pos.first].replaceIndex(pos.second, backup)
mcols[pos.second] = mcols[pos.second].replaceIndex(pos.first, backup)

result.takeIf { it != 0 }
}
}
}
}

private fun findReflection(input: List<String>, except: Int = -1): Int {
return (1 until input.size)
.filter { it != except }
.firstNotNullOfOrNull { divider ->
val size = min(divider, input.size - divider)
val top = input.subList(divider - size, divider)
val bottom = input.subList(divider, divider + size).reversed()
if (top == bottom)
divider
else null
} ?: 0
}
}

fun String.replaceIndex(idx: Int, char: Char): String {
return "${this.substring(0, idx)}$char${this.substring(idx+1)}"
}

fun coordinates(rows: Int, cols: Int): Sequence<Pair<Int, Int>> {
return sequence {
(0 until rows)
.forEach { r ->
(0 until cols)
.forEach { c ->
yield(r to c)
}
}
}
}

fun <T> Sequence<T>.split(predicate: (T) -> Boolean): Sequence<List<T>> {
val input = this
return sequence {
val buffer = mutableListOf<T>()
input.forEach { element ->
if (predicate(element)) {
yield(buffer.toList())
buffer.clear()
} else {
buffer.add(element)
}
}
if (buffer.isNotEmpty()) {
yield(buffer.toList())
}
}
}
38 changes: 34 additions & 4 deletions src/test/kotlin/de/linkel/aoc/Day13Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,42 @@ package de.linkel.aoc

class Day13Test: AbstractDayTest<Int>() {
override val exampleA = """
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#
""".trimIndent()
override val exampleSolutionA = 0
override val solutionA = 0
override val exampleSolutionA = 405
override val solutionA = 29213

override val exampleSolutionB = 0
override val solutionB = 0
override val exampleSolutionB = 400
override val solutionB = 37453

override val implementation = Day13()
}

/*
9
0 2 4 6 8
1 3 5 7
#...##..# 0
#....#..# 1
..##..### 2
#####.##. 3
#####.##. 4
..##..### 5
#....#..# 6
7
*/

0 comments on commit 113b355

Please sign in to comment.