-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay13.kt
57 lines (48 loc) · 1.77 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
private fun <T> Sequence<Pair<T, T>>.allEquals(): Boolean = all { (a, b) -> a == b }
private fun <T> equalsColumns(block: List<List<T>>, i: Int, j: Int): Boolean =
block.asSequence()
.map { it[i] }
.zip(block.asSequence().map { it[j] })
.allEquals()
private fun findReflectionLineScore(block: List<List<Boolean>>): Sequence<Int> {
val horizontal = block.asSequence()
.withIndex()
.zipWithNext()
.filter { (t, b) -> t.value == b.value }
.map { it.second.index }
.filter {
block.subList(0, it)
.asReversed()
.asSequence()
.zip(block.subList(it, block.size).asSequence())
.allEquals()
}
.map { it * 100 }
val vertical = block.first().indices.asSequence()
.zipWithNext()
.filter { (l, r) -> equalsColumns(block, l, r) }
.map { it.first }
.filter { index ->
(0..index).reversed().asSequence()
.zip((index + 1..<block.first().size).asSequence())
.all { (a, b) -> equalsColumns(block, a, b) }
}
.map { it + 1 }
return horizontal + vertical
}
fun main() {
val rockBlocks = mapBlocks { block -> block.map { line -> line.map { it == '#' } } }
val first = rockBlocks.sumOf { findReflectionLineScore(it).single() }
val second = rockBlocks.sumOf { block ->
val initial = findReflectionLineScore(block).single()
block.indexed2Sequence()
.flatMap { (index, _) ->
val copy = block.mutableDeepCopy()
copy[index] = !copy[index]
findReflectionLineScore(copy)
}
.filter { it != initial }
.first()
}
println("$first $second")
}