-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay19.kt
171 lines (142 loc) · 5.71 KB
/
Day19.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import kotlin.math.abs
private class Transformation(private val matrix: List<List<Int>>) {
init {
check(matrix.size == 3)
check(matrix.all { it.size == 3 })
}
constructor(rotation: List<Int>, permutation: List<Int>) : this(
List(3) { i ->
List(3) { j ->
if (permutation[j] == i) rotation[i] else 0
}
}
)
constructor() : this(List(3) { 1 }, List(3) { it })
// vector * matrix
infix fun apply(beaconCoordinates: BeaconCoordinates): BeaconCoordinates {
val (x, y, z) = beaconCoordinates
val list = listOf(x, y, z)
val (newX, newY, newZ) = List(3) { j ->
matrix
.asSequence()
.map { it[j] }
.zip(list.asSequence())
.map { (a, b) -> a * b }
.sum()
}
return BeaconCoordinates(newX, newY, newZ)
}
// matrix * matrix
infix fun compose(other: Transformation): Transformation {
val matrix = List(3) { i ->
List(3) { j ->
(0 until 3).asSequence()
.map { k -> matrix[i][k] * other.matrix[k][j] }
.sum()
}
}
return Transformation(matrix)
}
override fun toString() = matrix.joinToString("\n") { it.joinToString(" ", "(", ")") }
companion object {
private val ROTATIONS =
sequenceOf(-1, 1).flatMap { i ->
sequenceOf(-1, 1).flatMap { j ->
sequenceOf(-1, 1).map { k -> listOf(i, j, k) }
}
}
.toList()
private val PERMUTATIONS = listOf(
listOf(0, 1, 2),
listOf(0, 2, 1),
listOf(1, 0, 2),
listOf(1, 2, 0),
listOf(2, 0, 1),
listOf(2, 1, 0),
)
val ALL = (ROTATIONS cartesian PERMUTATIONS)
.map { (rotation, permutation) -> Transformation(rotation, permutation) }
}
}
private data class BeaconCoordinates(val x: Int, val y: Int, val z: Int) {
operator fun minus(other: BeaconCoordinates) = BeaconCoordinates(x - other.x, y - other.y, z - other.z)
operator fun plus(other: BeaconCoordinates) = BeaconCoordinates(x + other.x, y + other.y, z + other.z)
override fun toString() = "($x, $y, $z)"
companion object {
fun parse(string: String) = string.toInts().let { (x, y, z) -> BeaconCoordinates(x, y, z) }
}
}
private data class Scanner(val beaconCoordinates: List<BeaconCoordinates>) {
fun findOverlapping(other: Scanner): Pair<BeaconCoordinates, Transformation>? {
for ((thisPoint, otherPoint) in beaconCoordinates cartesian other.beaconCoordinates) {
for (transformation in Transformation.ALL) {
val base = thisPoint - (transformation apply otherPoint)
val rest = other.beaconCoordinates
.asSequence()
.map { transformation apply it }
.map { it + base }
.toSet()
.union(beaconCoordinates)
.size
if (beaconCoordinates.size + other.beaconCoordinates.size - rest >= 12) {
return base to transformation
}
}
}
return null
}
}
private fun findOverlappings(scanners: List<Scanner>): List<Pair<BeaconCoordinates, Transformation>> {
val transformations: MutableList<Pair<BeaconCoordinates, Transformation>?> = MutableList(scanners.size) { null }
transformations[0] = BeaconCoordinates(0, 0, 0) to Transformation()
val used = MutableList(scanners.size) { false }
var lastScanner = 0
while (transformations.any { it == null }) {
scanners
.asSequence()
.withIndex()
.filter { (index, _) -> transformations[index] == null }
.map { (index, value) -> index to scanners[lastScanner].findOverlapping(value) }
.filter { (_, value) -> value != null }
.forEach { (index, overlapping) ->
val (lastCoordinates, lastTransformation) = transformations[lastScanner]!!
val (newCoordinates, newTransformation) = overlapping!!
transformations[index] =
lastCoordinates + (lastTransformation apply newCoordinates) to
(newTransformation compose lastTransformation)
}
used[lastScanner] = true
lastScanner =
scanners.indices.asSequence().filter { transformations[it] != null && !used[it] }.firstOrNull() ?: 0
}
return transformations.map { it!! }
}
private fun findUniquePoints(scanners: List<Scanner>, overlappings: List<Pair<BeaconCoordinates, Transformation>>) =
scanners
.asSequence()
.zip(overlappings.asSequence())
.flatMap { (scanner, overlapping) ->
val (base, transformation) = overlapping
scanner.beaconCoordinates.asSequence().map { base + (transformation apply it) }
}
.distinct()
.count()
private fun findFurthestScanners(overlappings: List<Pair<BeaconCoordinates, Transformation>>): Int {
val bases = overlappings.map { it.first }
return (bases cartesian bases)
.map { (a, b) ->
val (x, y, z) = a - b
sequenceOf(x, y, z).map { abs(it) }.sum()
}
.maxOrNull()
.expect()
}
fun main() {
val scanners = withBlocks { block ->
val beaconCoordinates = block.asSequence().drop(1).map { BeaconCoordinates.parse(it) }.toList()
Scanner(beaconCoordinates)
}
val overlappings = findOverlappings(scanners)
println(findUniquePoints(scanners, overlappings))
println(findFurthestScanners(overlappings))
}