From 45fe0af1c7adca498660bef266bcf2f3db59cf8e Mon Sep 17 00:00:00 2001 From: Stephan Linkel <251381+norganos@users.noreply.github.com> Date: Mon, 11 Dec 2023 07:05:52 +0100 Subject: [PATCH] day 11 without Grid lib --- src/main/kotlin/de/linkel/aoc/Day11.kt | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/de/linkel/aoc/Day11.kt b/src/main/kotlin/de/linkel/aoc/Day11.kt index c799420..d4b1a5b 100644 --- a/src/main/kotlin/de/linkel/aoc/Day11.kt +++ b/src/main/kotlin/de/linkel/aoc/Day11.kt @@ -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 @@ -11,22 +10,27 @@ class Day11: AbstractLinesAdventDay() { override val day = 11 override fun process(part: QuizPart, lines: Sequence): 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() @@ -37,7 +41,6 @@ class Day11: AbstractLinesAdventDay() { ) } } - .sortedWith(compareBy({ it.y }, { it.x })) .toList() return galaxies .flatMapIndexed { idx, galaxy ->