Skip to content

Commit

Permalink
day 11 without Grid lib
Browse files Browse the repository at this point in the history
  • Loading branch information
norganos committed Dec 11, 2023
1 parent 7eba94b commit 45fe0af
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/main/kotlin/de/linkel/aoc/Day11.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package de.linkel.aoc

import de.linkel.aoc.base.AbstractLinesAdventDay
import de.linkel.aoc.base.QuizPart
import de.linkel.aoc.utils.grid.Grid
import de.linkel.aoc.utils.grid.Point
import jakarta.inject.Singleton

Expand All @@ -11,22 +10,27 @@ class Day11: AbstractLinesAdventDay<Long>() {
override val day = 11

override fun process(part: QuizPart, lines: Sequence<String>): Long {
val galaxies =
Grid.parse(lines) { _, char ->
if (char == '#') true else null
val galaxies = lines
.flatMapIndexed { y, line ->
line.toCharArray()
.mapIndexed { x, c ->
if (c == '#') Point(x, y) else null
}
.filterNotNull()
}
.let { grid ->
.toList()
.let { points ->
val width = points.maxOf { it.x } + 1
val height = points.maxOf { it.x } + 1
// part 2 example uses factor 100, part 2 puzzle 1.000.000...
// -1 because we don't multiply the line itself, but add copies, so we have to subtract the original
val expansionFactor = (if (part == QuizPart.A) 2 else if (grid.width == 10) 100 else 1_000_000) - 1
val expansionFactor = (if (part == QuizPart.A) 2 else if (width == 10) 100 else 1_000_000) - 1

val points = grid.getAllData()
.map { it.point }
val xx = (0 until grid.width).filter { x ->
val xx = (0 until width).filter { x ->
points.none { it.x == x }
}
.sortedDescending()
val yy = (0 until grid.height).filter { y ->
val yy = (0 until height).filter { y ->
points.none { it.y == y }
}
.sortedDescending()
Expand All @@ -37,7 +41,6 @@ class Day11: AbstractLinesAdventDay<Long>() {
)
}
}
.sortedWith(compareBy({ it.y }, { it.x }))
.toList()
return galaxies
.flatMapIndexed { idx, galaxy ->
Expand Down

0 comments on commit 45fe0af

Please sign in to comment.