V den 6. jsem se pokusil zaměřit se na rychlost řešení, namísto čistého kódu, abych odhadl, jestli bych měl vůbec šanci dostat se do Top 100 v leaderboardu. No, trvalo mi to něco přes 40 min, kde se mi u toho málem roztopil mozek 🙂 . Takže na to prdím, neb to mi za to nestojí … zde je moje řešení pro den 6. a příště se navrátím zpět k důmyslnosti a čistotě řešení samotného:
import com.svetylkovo.rojo.Rojo
import java.io.File
import kotlin.math.abs
import kotlin.streams.toList
data class Coord(val id: Int, val x: Int, val y: Int) {
fun manhattanDistanceFrom(other: Coord) = abs(x - other.x) + abs(y - other.y)
}
fun main() {
val input = File("input6.txt").readText().trim()
val coords = Rojo.map("(\\d+), (\\d+)", input) { x, y ->
x.toInt() to y.toInt()
}.toList().mapIndexed { index, it ->
Coord(index + 1, it.first, it.second)
}
val maxX = (coords.map { it.x }.max() ?: 0) + 1
val maxY = (coords.map { it.y }.max() ?: 0) + 1
val area = Array(maxX) { IntArray(maxY) { 0 } }
for (x in 0 until maxX) {
for (y in 0 until maxY) {
area[x][y] = findClosestDistance(x, y, coords)
}
}
val infiniteAreas = (0 until maxX).map { x ->
(0 until maxY).map { y ->
if (x == 0 || x == maxX - 1 || y == 0 || y == maxY - 1) area[x][y] else null
}
}.flatten().filterNotNull().filter {
it != 0
}.distinct()
val largestNonInfinite = coords.filterNot { it.id in infiniteAreas }
.map { findRegionSize(area, it) }.max()
println(largestNonInfinite)
//part B
for (x in 0 until maxX) {
for (y in 0 until maxY) {
if (findSummedDistance(x, y, coords) < 10000) {
area[x][y] = -1
}
}
}
val regionSize = area.flatMap { it.toList() }.count { it == -1 }
println(regionSize)
}
private fun findRegionSize(
area: Array<IntArray>,
coord: Coord
): Int {
return area.sumBy {
it.count { it == coord.id }
}
}
fun findSummedDistance(x: Int, y: Int, coords: List<Coord>): Int {
val coord = Coord(0, x, y)
val distancesMap = coords.associateWith { it.manhattanDistanceFrom(coord) }
return distancesMap.values.sum()
}
fun findClosestDistance(x: Int, y: Int, coords: List<Coord>): Int {
val coord = Coord(0, x, y)
val distancesMap = coords.associateWith { it.manhattanDistanceFrom(coord) }
val closest = distancesMap.minBy { it.value }
if (closest != null) {
val closestCount = distancesMap.values
.filter { it == closest.value }
.count()
return if (closestCount > 1) 0 else closest.key.id
}
return 0
}