-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathy23day13part2.kt
More file actions
22 lines (22 loc) · 1.1 KB
/
Copy pathy23day13part2.kt
File metadata and controls
22 lines (22 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
fun main() {
fun readPattern() = generateSequence { readlnOrNull()?.takeIf { it.isNotEmpty() } }.toList()
fun List<String>.transpose(): List<String> =
this[0].indices.map { j -> this.indices.joinToString("") { i -> this[i][j].toString() } }
infix fun String.diffCount(that: String) = indices.count { this[it] != that[it] }
fun List<String>.smudgySymmetrical(count: Int): Boolean {
var smudge = 0
return (0 until count).all {
count + it >= size ||
(get(count - it - 1) diffCount get(count + it))
.let { it == 0 || it == 1 && smudge++ == 0 }
} && smudge == 1
}
fun List<String>.countToReflection(): Int =
indices.run { start + 1 .. endInclusive }.filter { smudgySymmetrical(it) }.sum()
generateSequence { readPattern().takeIf { it.isNotEmpty() } }.sumOf { pattern ->
val horizontalCount = pattern.countToReflection()
val verticalCount = pattern.transpose().countToReflection()
println("$horizontalCount, $verticalCount")
100 * horizontalCount + verticalCount
}.let(::println)
}