Skip to content

Commit

Permalink
Solution for 2023-12-11, part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
pkoziol committed Dec 11, 2023
1 parent 91e528e commit 7a0b982
Show file tree
Hide file tree
Showing 3 changed files with 298 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/main/kotlin/biz/koziolek/adventofcode/year2023/day11/day11.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package biz.koziolek.adventofcode.year2023.day11

import biz.koziolek.adventofcode.Coord
import biz.koziolek.adventofcode.findInput

fun main() {
val inputFile = findInput(object {})
val map = parseGalaxyMap(inputFile.bufferedReader().readLines())
println("Sum of shortest paths between every pair in expanded universe is: ${map.expand().distances.sum()}")
}

const val GALAXY = '#'
const val VOID = '.'

data class GalaxyMap(val galaxies: Set<Coord>) {
val width = galaxies.maxOf { it.x } + 1
val height = galaxies.maxOf { it.y } + 1
val distances: List<Int> by lazy {
val galaxiesList = galaxies.toList()
galaxiesList
.flatMapIndexed { firstIdx, firstCoord ->
galaxiesList
.filterIndexed { secondIndex, _ -> secondIndex > firstIdx }
.map { secondCoord -> firstCoord to secondCoord }
}
.map { it.first.manhattanDistanceTo(it.second) }
}

fun expand(): GalaxyMap {
val emptyColumns = (0..width) - galaxies.map { it.x }.toSet()
val emptyRows = (0..height) - galaxies.map { it.y }.toSet()

return GalaxyMap(
galaxies = galaxies.map { oldCoord ->
Coord(
x = oldCoord.x + emptyColumns.count { it < oldCoord.x },
y = oldCoord.y + emptyRows.count { it < oldCoord.y },
)
}.toSet(),
)
}

override fun toString() =
buildString {
for (y in 0 until height) {
for (x in 0 until width) {
val coord = Coord(x, y)

if (coord in galaxies) {
append(GALAXY)
} else {
append(VOID)
}
}

if (y != height - 1) {
append("\n")
}
}
}
}

fun parseGalaxyMap(lines: Iterable<String>): GalaxyMap =
lines.flatMapIndexed { y, line ->
line.mapIndexedNotNull { x, char ->
if (char == GALAXY) {
Coord(x, y)
} else {
null
}
}
}.let { GalaxyMap(it.toSet()) }
Loading

0 comments on commit 7a0b982

Please sign in to comment.