Day 11 cleanup

This commit is contained in:
2018-12-11 17:38:14 +01:00
parent 4ebdaf7b3d
commit 1730dfe955

View File

@@ -1,7 +1,5 @@
package com.basdado.adventofcode package com.basdado.adventofcode
import kotlin.math.roundToInt
import kotlin.math.sqrt
import kotlin.system.measureTimeMillis import kotlin.system.measureTimeMillis
fun main() { fun main() {
@@ -87,13 +85,14 @@ class Day11 {
} }
class PowerCell(val x: Int, val y: Int, val gridSerial: Int) { class PowerCell(val x: Int, val y: Int, val gridSerial: Int) {
val rackId = x + 10
private val rackId = x + 10
val powerLevel = hundreds(((rackId * y) + gridSerial) * rackId) - 5 val powerLevel = hundreds(((rackId * y) + gridSerial) * rackId) - 5
} }
class PowerBlockCache(val maxSize: Int, val maxX: Int, val maxY: Int) { class PowerBlockCache(maxSize: Int, private val maxX: Int, private val maxY: Int) {
val cache = Array(maxSize) { size -> Array(maxX - size) { Array<Int?>(maxY - size) {null}}}
private val cache = Array(maxSize) { size -> Array(maxX - size) { Array<Int?>(maxY - size) {null}}}
fun get(x: Int, y: Int, size: Int): Int? { fun get(x: Int, y: Int, size: Int): Int? {
return cache[size - 1][x][y] return cache[size - 1][x][y]
@@ -109,15 +108,5 @@ class Day11 {
fun hundreds(x: Int): Int { fun hundreds(x: Int): Int {
return (x - ((x / 1000) * 1000)) / 100 return (x - ((x / 1000) * 1000)) / 100
} }
@JvmStatic
fun divisor(n: Int): Int? {
return DEFAULT_DIVISORS.find { n % it == 0 && it < n } ?:
(DEFAULT_DIVISORS.last()..sqrt(n.toDouble()).roundToInt()).find { n % it == 0 }
}
private val DEFAULT_DIVISORS = intArrayOf(2, 3, 5, 7, 11, 13, 17, 19)
} }
data class PowerBlock(val x: Int, val y: Int, val size: Int)
} }