generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day13.kt
68 lines (58 loc) · 2.41 KB
/
Day13.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package io.liodev.aoc.aoc2023
import io.liodev.aoc.Day
import io.liodev.aoc.readInputAsString
import io.liodev.aoc.runDay
// --- 2023 Day 13: Point of Incidence ---
class Day13(input: String) : Day<Int> {
override val expectedValues = listOf(405, 34821, 400, 36919)
private val patterns = input.split("\n\n").map { it.lines() }
private fun List<String>.findReflectingRow(smudges: Int = 0): Int {
for (i in 0..<lastIndex) {
if (differences(this[i], this[i + 1]) <= smudges) {
var u = i
var d = i + 1
var sm = 0
while (u >= 0 && d <= lastIndex) {
sm += differences(this[u], this[d])
if (sm > smudges) break
u--; d++
}
if (sm == smudges) return i + 1
}
}
return -1
}
private fun differences(s1: String, s2: String): Int {
return s1.zip(s2).count { (c1, c2) -> c1 != c2 }
}
private fun List<String>.findReflectingCol(smudges: Int = 0): Int {
val transpose = this[0].indices.map { j ->
this.indices.map { i -> this[i][j] }.joinToString("") { it.toString() }
}
return transpose.findReflectingRow(smudges)
}
override fun solvePart1(): Int =
patterns.map { pattern ->
val mirrorRow = pattern.findReflectingRow()
val mirrorCol = if (mirrorRow == -1) pattern.findReflectingCol() else -1
if (mirrorRow == -1 && mirrorCol == -1) error("No line of reflection in:\n $pattern")
Pair(mirrorRow, mirrorCol)
}.sumOf { (r, c) ->
(if (r != -1) r else 0) * 100 + (if (c != -1) c else 0)
}
override fun solvePart2(): Int =
patterns.map { pattern ->
val mirrorRow = pattern.findReflectingRow(1)
val mirrorCol = if (mirrorRow == -1) pattern.findReflectingCol(1) else -1
if (mirrorRow == -1 && mirrorCol == -1) error("No line of reflection in:\n $pattern")
Pair(mirrorRow, mirrorCol)
}.sumOf { (r, c) ->
(if (r != -1) r else 0) * 100 + (if (c != -1) c else 0)
}
}
fun main() {
val name = Day13::class.simpleName
val testInput = readInputAsString("src/input/2023/${name}_test.txt")
val realInput = readInputAsString("src/input/2023/${name}.txt")
runDay(Day13(testInput), Day13(realInput), printTimings = true)
}