From 6b134b55c5f59c400f2edbb17ac4d872d63d521a Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Wed, 13 Dec 2023 08:08:31 +0100 Subject: [PATCH] day 13 puzzle 2 --- README.md | 16 +++--- src/day13/Puzzle2.scala | 94 +++++++++++++++++++++++++++++++++++ tests/day13/Puzzle2Test.scala | 9 ++++ 3 files changed, 111 insertions(+), 8 deletions(-) create mode 100644 src/day13/Puzzle2.scala create mode 100644 tests/day13/Puzzle2Test.scala diff --git a/README.md b/README.md index a9f4073..e967055 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: 23 / 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
:star: | 14 | 15 | 16 | 17 | -| 18 | 19 | 20 | 21 | 22 | 23 | 24 | -| 25 | | | | | | | \ No newline at end of file +#### Stars: 24 / 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
:star::star: | 14 | 15 | 16 | 17 | +| 18 | 19 | 20 | 21 | 22 | 23 | 24 | +| 25 | | | | | | | \ No newline at end of file diff --git a/src/day13/Puzzle2.scala b/src/day13/Puzzle2.scala new file mode 100644 index 0000000..d39e1b6 --- /dev/null +++ b/src/day13/Puzzle2.scala @@ -0,0 +1,94 @@ +package day13 + +import scala.io.{BufferedSource, Source} + +object Puzzle2 { + var maps: Array[Array[Array[Boolean]]] = Array.empty + def loadInput(path: String): Unit = { + val source: BufferedSource = Source.fromFile(path) + val lines: String = source.getLines().mkString("\n") + + val mapsStr: Array[String] = lines.split("\n\n") + maps = new Array(mapsStr.length) + + for ((mapStr: String, i: Int) <- mapsStr.zipWithIndex) { + val rows: Array[String] = mapStr.split("\n") + val map: Array[Array[Boolean]] = new Array(rows.length) + for ((row: String, j: Int) <- rows.zipWithIndex) { + map(j) = row.map(_ == '#').toArray + } + maps(i) = map + } + + source.close() + } + + def getReflectionValue(map: Array[Array[Boolean]]): Int = { + val height: Int = map.length + val width: Int = if (height == 0) 0 else map(0).length + var value: Int = 0 + + for (x: Int <- 0 until width-1) { + if (isReflection(map, x)) { + value += x+1 + } + } + + val mapT: Array[Array[Boolean]] = transpose(map) + + for (y: Int <- 0 until height-1) { + if (isReflection(mapT, y)) { + value += 100*(y+1) + } + } + + return value + } + + def isReflection(map: Array[Array[Boolean]], x: Int): Boolean = { + var diffs: Int = 0 + for (row: Array[Boolean] <- map) { + val a = row.dropRight(row.length - x - 1).reverse + val b = row.drop(x+1) + for (i: Int <- 0 until Math.min(a.length, b.length)) { + if (a(i) != b(i)) { + diffs += 1 + if (diffs > 1) return false + } + } + } + return diffs == 1 + } + + def transpose(map: Array[Array[Boolean]]): Array[Array[Boolean]] = { + val height: Int = map.length + val width: Int = if (height == 0) 0 else map(0).length + + val mapT: Array[Array[Boolean]] = Array.ofDim(width, height) + + for (y: Int <- 0 until height) { + for (x: Int <- 0 until width) { + mapT(x)(y) = map(y)(x) + } + } + + return mapT + } + + def solve(path: String): Int = { + loadInput(path) + + var solution: Int = 0 + + for (map: Array[Array[Boolean]] <- maps) { + solution += getReflectionValue(map) + } + + return solution + } + + def main(args: Array[String]): Unit = { + val solution: Int = solve("./res/day13/input1.txt") + println(solution) + } +} diff --git a/tests/day13/Puzzle2Test.scala b/tests/day13/Puzzle2Test.scala new file mode 100644 index 0000000..f77240c --- /dev/null +++ b/tests/day13/Puzzle2Test.scala @@ -0,0 +1,9 @@ +package day13 + +import org.scalatest.funsuite.AnyFunSuite + +class Puzzle2Test extends AnyFunSuite { + test("Puzzle2.solve") { + assert(Puzzle2.solve("tests_res/day13/input1.txt") == 400) + } +}