38 lines
1.2 KiB
Kotlin
38 lines
1.2 KiB
Kotlin
import java.io.File
|
|
import kotlin.math.absoluteValue
|
|
|
|
data class Particle(var x: Long, var y: Long, var z: Long,
|
|
var vx: Long, var vy: Long, var vz: Long,
|
|
var ax: Long, var ay: Long, var az: Long) {
|
|
fun distance() = x.absoluteValue + y.absoluteValue + z.absoluteValue
|
|
fun step(){
|
|
vx += ax
|
|
vy += ay
|
|
vz += az
|
|
x += vx
|
|
y += vy
|
|
z += vz
|
|
}
|
|
}
|
|
|
|
fun main(args: Array<String>){
|
|
val file = File("../puzzle_20.txt")
|
|
// val file = File("test.txt")
|
|
val lines = file.readLines()
|
|
val firstLine = lines.first()
|
|
val particles = lines.map {
|
|
val split = it.split("=")
|
|
val (x, y, z) = split[1].substring(1).substringBefore(">").split(",").map { it.toLong() }
|
|
val (vx, vy, vz) = split[2].substring(1).substringBefore(">").split(",").map { it.toLong() }
|
|
val (ax, ay, az) = split[3].substring(1).substringBefore(">").split(",").map { it.toLong() }
|
|
Particle(x, y, z, vx, vy, vz, ax, ay, az)
|
|
}
|
|
var count = 0
|
|
while(count < 1000){
|
|
particles.forEach {
|
|
it.step()
|
|
}
|
|
count++
|
|
}
|
|
println(particles.indexOf(particles.minBy { it.distance() }))
|
|
} |