AdventOfCode-2017/day20_2.kt

40 lines
1.4 KiB
Kotlin

import java.io.File
import kotlin.math.absoluteValue
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)
}.toMutableList()
var count = 0
while(count < 1000){
particles.forEach {
it.step()
}
val collidedParticles = mutableListOf<Particle>()
var index = 0
while(index < particles.size){
val par1 = particles[index]
var index2 = index + 1;
while(index2 < particles.size){
val par2 = particles[index2]
if(par1.x == par2.x && par1.y == par2.y && par1.z == par2.z) {
collidedParticles.add(par1)
collidedParticles.add(par2)
}
index2++
}
index++
}
particles.removeAll(collidedParticles)
count++
}
println(particles.size)
}