Skip to content

Commit

Permalink
Deduplicate 2023 day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 11, 2023
1 parent ff69ce0 commit 746abfc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
50 changes: 24 additions & 26 deletions src/main/scala/eu/sim642/adventofcode2023/Day11.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)))
}
}
10 changes: 5 additions & 5 deletions src/test/scala/eu/sim642/adventofcode2023/Day11Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 746abfc

Please sign in to comment.