From 0fb66d1bc786a33a99e2b43544c4e6a4ad54d27c Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Wed, 13 Dec 2023 07:50:11 +0100 Subject: [PATCH] day 13 puzzle 1 --- README.md | 4 +- src/day13/Puzzle1.scala | 92 +++++++++++++++++++++++++++++++++++ tests/day13/Puzzle1Test.scala | 9 ++++ tests_res/day13/input1.txt | 15 ++++++ 4 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 src/day13/Puzzle1.scala create mode 100644 tests/day13/Puzzle1Test.scala create mode 100644 tests_res/day13/input1.txt diff --git a/README.md b/README.md index ac915cf..a9f4073 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: 22 / 50 +#### 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 | 14 | 15 | 16 | 17 | +| 11
:star::star: | 12 | 13
:star: | 14 | 15 | 16 | 17 | | 18 | 19 | 20 | 21 | 22 | 23 | 24 | | 25 | | | | | | | \ No newline at end of file diff --git a/src/day13/Puzzle1.scala b/src/day13/Puzzle1.scala new file mode 100644 index 0000000..756e9bb --- /dev/null +++ b/src/day13/Puzzle1.scala @@ -0,0 +1,92 @@ +package day13 + +import scala.io.{BufferedSource, Source} + +object Puzzle1 { + 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 = { + 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)) { + return false + } + } + } + return true + } + + 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/Puzzle1Test.scala b/tests/day13/Puzzle1Test.scala new file mode 100644 index 0000000..3f4548b --- /dev/null +++ b/tests/day13/Puzzle1Test.scala @@ -0,0 +1,9 @@ +package day13 + +import org.scalatest.funsuite.AnyFunSuite + +class Puzzle1Test extends AnyFunSuite { + test("Puzzle1.solve") { + assert(Puzzle1.solve("tests_res/day13/input1.txt") == 405) + } +} diff --git a/tests_res/day13/input1.txt b/tests_res/day13/input1.txt new file mode 100644 index 0000000..f226414 --- /dev/null +++ b/tests_res/day13/input1.txt @@ -0,0 +1,15 @@ +#.##..##. +..#.##.#. +##......# +##......# +..#.##.#. +..##..##. +#.#.##.#. + +#...##..# +#....#..# +..##..### +#####.##. +#####.##. +..##..### +#....#..# \ No newline at end of file