Day 10 code cleanup

This commit is contained in:
2019-12-12 01:05:27 +01:00
parent fd474aa6fb
commit baa5db467f

View File

@@ -10,23 +10,6 @@ import kotlin.math.min
const val DAY10_INPUT_PATH = "/day/10/input.txt"
fun main() {
// val circle = listOf(
// Day10.Vector2i(0, 1),
// Day10.Vector2i(1, 1),
// Day10.Vector2i(1, 0),
// Day10.Vector2i(0, -1),
// Day10.Vector2i(-1, -1),
// Day10.Vector2i(-1, 0),
// Day10.Vector2i(-1, 1)
// )
//
// circle.map {
// - (it.angle() + 0.5 * PI)
// }.map { if (it < 0) it + 2 * PI else it }.forEach(::println)
// println(Day10.Vector2i(5, 2).angle())
Day10.puzzle1()
Day10.puzzle2()
}
@@ -42,20 +25,14 @@ object Day10 {
fun puzzle2() {
val asteroids = loadAsteroids()
val optimalAsteroid = optimalAsteroidsPerDirection(asteroids)
// val optimalAsteroid = Pair(Vector2i(8, 3), asteroidsPerDirection(Vector2i(8, 3), asteroids))
// println(optimalAsteroid.first)
// optimalAsteroid.second.keys.forEach {
// var angle = (it.angle() + 0.5 * PI);
// angle = if (angle < 0) 2*PI + angle else angle
// println("(${it.x}, ${it.y}) = $angle")
// }
val sortedAsteroidLists = optimalAsteroid.second
.entries
.sortedBy { val angle = (it.key.angle() + 0.5 * PI); if (angle < 0) 2*PI + angle else angle }
.map { it.value.sortedBy( Vector2i::manhattan ) }
// And now we're gonna keep taking one of each list until we reach number 200
var listIndex = 0
var itemIndexPerList = IntArray(sortedAsteroidLists.size)
val itemIndexPerList = IntArray(sortedAsteroidLists.size)
var i = 0
var curItem: Vector2i? = null
while (i < 200) {
@@ -108,8 +85,6 @@ object Day10 {
operator fun plus(v: Vector2i) = Vector2i(x + v.x, y + v.y)
}
val ORIGIN = Vector2i(0, 0)
fun gcd(n1: Int, n2: Int) = when {
n1 == 0 && n2 == 0 -> 1 // Actually undefined/any number, but whatever
n1 == 0 -> abs(n2)