From 746abfc9e58d0be0bc0b1d9e8bfa2f7f87e18a77 Mon Sep 17 00:00:00 2001 From: Simmo Saan Date: Mon, 11 Dec 2023 08:37:57 +0200 Subject: [PATCH] Deduplicate 2023 day 11 --- .../eu/sim642/adventofcode2023/Day11.scala | 50 +++++++++---------- .../sim642/adventofcode2023/Day11Test.scala | 10 ++-- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/src/main/scala/eu/sim642/adventofcode2023/Day11.scala b/src/main/scala/eu/sim642/adventofcode2023/Day11.scala index 697dadbb..1e937058 100644 --- a/src/main/scala/eu/sim642/adventofcode2023/Day11.scala +++ b/src/main/scala/eu/sim642/adventofcode2023/Day11.scala @@ -5,35 +5,33 @@ import eu.sim642.adventofcodelib.pos.Pos object Day11 { - def sumDistances(galaxies: Seq[Pos]): Int = { - val xs = galaxies.map(_.x).toSet - val freeXs = (0 to xs.max).toSet -- xs - val ys = galaxies.map(_.y).toSet - val freeYs = (0 to ys.max).toSet -- ys - - (for { - (galaxy1, i) <- galaxies.view.zipWithIndex - galaxy2 <- galaxies.view.drop(i + 1) - galaxyMin = galaxy1 min galaxy2 - galaxyMax = galaxy1 max galaxy2 - } yield (galaxy1 manhattanDistance galaxy2) + freeXs.count(x => galaxyMin.x < x && x < galaxyMax.x) + freeYs.count(y => galaxyMin.y < y && y < galaxyMax.y)).sum + trait Part { + val defaultExpansionFactor: Int + + def sumDistances(galaxies: Seq[Pos], expansionFactor: Long = defaultExpansionFactor): Long = { + val xs = galaxies.map(_.x).toSet + val freeXs = (0 to xs.max).toSet -- xs + val ys = galaxies.map(_.y).toSet + val freeYs = (0 to ys.max).toSet -- ys + + (for { + (galaxy1, i) <- galaxies.view.zipWithIndex + galaxy2 <- galaxies.view.drop(i + 1) + galaxyMin = galaxy1 min galaxy2 + galaxyMax = galaxy1 max galaxy2 + } yield (galaxy1 manhattanDistance galaxy2) + (expansionFactor - 1) * freeXs.count(x => galaxyMin.x < x && x < galaxyMax.x) + (expansionFactor - 1) * freeYs.count(y => galaxyMin.y < y && y < galaxyMax.y)).sum + } } - // TODO: deduplicate - def sumDistances2(galaxies: Seq[Pos], factor: Long = 1000000L): Long = { - val xs = galaxies.map(_.x).toSet - val freeXs = (0 to xs.max).toSet -- xs - val ys = galaxies.map(_.y).toSet - val freeYs = (0 to ys.max).toSet -- ys + object Part1 extends Part { + override val defaultExpansionFactor: Int = 2 + } - (for { - (galaxy1, i) <- galaxies.view.zipWithIndex - galaxy2 <- galaxies.view.drop(i + 1) - galaxyMin = galaxy1 min galaxy2 - galaxyMax = galaxy1 max galaxy2 - } yield (galaxy1 manhattanDistance galaxy2) + (factor - 1) * freeXs.count(x => galaxyMin.x < x && x < galaxyMax.x) + (factor - 1) * freeYs.count(y => galaxyMin.y < y && y < galaxyMax.y)).sum + object Part2 extends Part { + override val defaultExpansionFactor: Int = 1000000 } + def parseGalaxies(input: String): Seq[Pos] = { val grid = input.linesIterator.map(_.toVector).toVector (for { @@ -46,7 +44,7 @@ object Day11 { lazy val input: String = scala.io.Source.fromInputStream(getClass.getResourceAsStream("day11.txt")).mkString.trim def main(args: Array[String]): Unit = { - println(sumDistances(parseGalaxies(input))) - println(sumDistances2(parseGalaxies(input))) + println(Part1.sumDistances(parseGalaxies(input))) + println(Part2.sumDistances(parseGalaxies(input))) } } diff --git a/src/test/scala/eu/sim642/adventofcode2023/Day11Test.scala b/src/test/scala/eu/sim642/adventofcode2023/Day11Test.scala index 6bbb9b06..f333c91b 100644 --- a/src/test/scala/eu/sim642/adventofcode2023/Day11Test.scala +++ b/src/test/scala/eu/sim642/adventofcode2023/Day11Test.scala @@ -18,19 +18,19 @@ class Day11Test extends AnyFunSuite { |#...#.....""".stripMargin test("Part 1 examples") { - assert(sumDistances(parseGalaxies(exampleInput)) == 374) + assert(Part1.sumDistances(parseGalaxies(exampleInput)) == 374) } test("Part 1 input answer") { - assert(sumDistances(parseGalaxies(input)) == 9509330) + assert(Part1.sumDistances(parseGalaxies(input)) == 9509330) } test("Part 2 examples") { - assert(sumDistances2(parseGalaxies(exampleInput), 10) == 1030) - assert(sumDistances2(parseGalaxies(exampleInput), 100) == 8410) + assert(Part2.sumDistances(parseGalaxies(exampleInput), 10) == 1030) + assert(Part2.sumDistances(parseGalaxies(exampleInput), 100) == 8410) } test("Part 2 input answer") { - assert(sumDistances2(parseGalaxies(input)) == 635832237682L) + assert(Part2.sumDistances(parseGalaxies(input)) == 635832237682L) } }