From c693cd9259c7d96069ddc1b14837caf7cb874dc3 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Mon, 11 Dec 2023 07:45:01 +0100 Subject: [PATCH] day 11 puzzle 2 --- README.md | 16 +++---- src/day11/Puzzle2.scala | 82 +++++++++++++++++++++++++++++++++++ tests/day11/Puzzle2Test.scala | 12 +++++ 3 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 src/day11/Puzzle2.scala create mode 100644 tests/day11/Puzzle2Test.scala diff --git a/README.md b/README.md index 27055c2..ac915cf 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,11 @@ This repo contains my attempt at this year's Advent of Code (2023) I will try and do some problems (probably not all of them) in Scala ## Progress: -#### Stars: 21 / 50 -| Mon | Tue | Wed | Thu | Fri | Sat | Sun | -|:-----------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:|:------------------:| -| | | | | 1
:star::star: | 2
:star::star: | 3
:star::star: | -| 4
:star::star: | 5
:star::star: | 6
:star::star: | 7
:star::star: | 8
:star::star: | 9
:star::star: | 10
:star::star: | -| 11
:star: | 12 | 13 | 14 | 15 | 16 | 17 | -| 18 | 19 | 20 | 21 | 22 | 23 | 24 | -| 25 | | | | | | | \ No newline at end of file +#### Stars: 22 / 50 +| Mon | Tue | Wed | Thu | Fri | Sat | Sun | +|:------------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:|:------------------:| +| | | | | 1
:star::star: | 2
:star::star: | 3
:star::star: | +| 4
:star::star: | 5
:star::star: | 6
:star::star: | 7
:star::star: | 8
:star::star: | 9
:star::star: | 10
:star::star: | +| 11
:star::star: | 12 | 13 | 14 | 15 | 16 | 17 | +| 18 | 19 | 20 | 21 | 22 | 23 | 24 | +| 25 | | | | | | | \ No newline at end of file diff --git a/src/day11/Puzzle2.scala b/src/day11/Puzzle2.scala new file mode 100644 index 0000000..49fdb7b --- /dev/null +++ b/src/day11/Puzzle2.scala @@ -0,0 +1,82 @@ +package day11 + +import scala.collection.mutable.ArrayBuffer +import scala.io.{BufferedSource, Source} + +object Puzzle2 { + var ogUniverse: Array[Array[Boolean]] = Array.empty + var ogHeight: Int = 0 + var ogWidth: Int = 0 + var colCounts: Array[Int] = Array.empty + var rowCounts: Array[Int] = Array.empty + var galaxies: ArrayBuffer[(Long, Long)] = new ArrayBuffer() + def loadInput(path: String): Unit = { + val source: BufferedSource = Source.fromFile(path) + val lines: Array[String] = source.getLines().toArray + + ogHeight = lines.length + ogWidth = if (ogHeight == 0) 0 else lines(0).length + ogUniverse = Array.ofDim(ogHeight, ogWidth) + + colCounts = new Array(ogWidth) + rowCounts = new Array(ogHeight) + + for ((line: String, y: Int) <- lines.zipWithIndex) { + for ((c: Char, x: Int) <- line.zipWithIndex) { + if (c == '#') { + ogUniverse(y)(x) = true + colCounts(x) += 1 + rowCounts(y) += 1 + } + } + } + + source.close() + } + + def findGalaxiesRealPos(age: Int): Unit = { + galaxies = new ArrayBuffer() + + var realX: Long = 0 + var realY: Long = 0 + + for (ogY: Int <- 0 until ogHeight) { + realX = 0 + for (ogX: Int <- 0 until ogWidth) { + if (ogUniverse(ogY)(ogX)) { + galaxies.addOne((realX, realY)) + } + if (colCounts(ogX) == 0) realX += age-1 + realX += 1 + } + if (rowCounts(ogY) == 0) realY += age-1 + realY += 1 + } + } + + def distance(g1: (Long, Long), g2: (Long, Long)): Long = { + return Math.abs(g2._1 - g1._1) + Math.abs(g2._2 - g1._2) + } + + def solve(path: String, age: Int=2): Long = { + loadInput(path) + findGalaxiesRealPos(age) + + var solution: Long = 0 + + for (i: Int <- galaxies.indices) { + val g1: (Long, Long) = galaxies(i) + for (j: Int <- i+1 until galaxies.length) { + val g2: (Long, Long) = galaxies(j) + solution += distance(g1, g2) + } + } + + return solution + } + + def main(args: Array[String]): Unit = { + val solution: Long = solve("./res/day11/input1.txt", 1000000) + println(solution) + } +} diff --git a/tests/day11/Puzzle2Test.scala b/tests/day11/Puzzle2Test.scala new file mode 100644 index 0000000..1f0de98 --- /dev/null +++ b/tests/day11/Puzzle2Test.scala @@ -0,0 +1,12 @@ +package day11 + +import org.scalatest.funsuite.AnyFunSuite + +class Puzzle2Test extends AnyFunSuite { + test("Puzzle2.solve 1") { + assert(Puzzle2.solve("tests_res/day11/input1.txt", 10) == 1030) + } + test("Puzzle2.solve 2") { + assert(Puzzle2.solve("tests_res/day11/input1.txt", 100) == 8410) + } +}