From d36d403402b49fe696b87f5f5756efce48178c15 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Thu, 14 Dec 2023 07:37:10 +0100 Subject: [PATCH] day 14 puzzle 1 --- README.md | 4 +-- src/day14/Puzzle1.scala | 68 +++++++++++++++++++++++++++++++++++ tests/day14/Puzzle1Test.scala | 9 +++++ tests_res/day14/input1.txt | 10 ++++++ 4 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 src/day14/Puzzle1.scala create mode 100644 tests/day14/Puzzle1Test.scala create mode 100644 tests_res/day14/input1.txt diff --git a/README.md b/README.md index e967055..b6b7737 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: 24 / 50 +#### Stars: 25 / 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 | +| 11
:star::star: | 12 | 13
:star::star: | 14
:star: | 15 | 16 | 17 | | 18 | 19 | 20 | 21 | 22 | 23 | 24 | | 25 | | | | | | | \ No newline at end of file diff --git a/src/day14/Puzzle1.scala b/src/day14/Puzzle1.scala new file mode 100644 index 0000000..11ec103 --- /dev/null +++ b/src/day14/Puzzle1.scala @@ -0,0 +1,68 @@ +package day14 + +import scala.io.{BufferedSource, Source} + +object Puzzle1 { + val CHARS: Array[Char] = Array('.', 'O', '#') + var platform: Array[Array[Int]] = Array.empty + var width: Int = 0 + var height: Int = 0 + + def loadInput(path: String): Unit = { + val source: BufferedSource = Source.fromFile(path) + val lines: Array[String] = source.getLines().toArray + + platform = lines.map(_.map(CHARS.indexOf(_)).toArray) + height = platform.length + width = if (height == 0) 0 else platform(0).length + + source.close() + } + + def slideUp(): Unit = { + for (y: Int <- 0 until height) { + for (x: Int <- 0 until width) { + if (platform(y)(x) == 1) { + slideRockUp(x, y) + } + } + } + } + + def slideRockUp(x: Int, y: Int): Unit = { + platform(y)(x) = 0 + for (y2: Int <- y-1 to 0 by -1) { + if (platform(y2)(x) != 0) { + platform(y2+1)(x) = 1 + return + } + } + platform(0)(x) = 1 + } + + def computeLoad(): Int = { + var load: Int = 0 + for (y: Int <- 0 until height) { + for (x: Int <- 0 until width) { + if (platform(y)(x) == 1) { + load += height-y + } + } + } + return load + } + + def solve(path: String): Int = { + loadInput(path) + slideUp() + + val solution: Int = computeLoad() + + return solution + } + + def main(args: Array[String]): Unit = { + val solution: Int = solve("./res/day14/input1.txt") + println(solution) + } +} diff --git a/tests/day14/Puzzle1Test.scala b/tests/day14/Puzzle1Test.scala new file mode 100644 index 0000000..09771e8 --- /dev/null +++ b/tests/day14/Puzzle1Test.scala @@ -0,0 +1,9 @@ +package day14 + +import org.scalatest.funsuite.AnyFunSuite + +class Puzzle1Test extends AnyFunSuite { + test("Puzzle1.solve") { + assert(Puzzle1.solve("tests_res/day14/input1.txt") == 136) + } +} diff --git a/tests_res/day14/input1.txt b/tests_res/day14/input1.txt new file mode 100644 index 0000000..b92d1a3 --- /dev/null +++ b/tests_res/day14/input1.txt @@ -0,0 +1,10 @@ +O....#.... +O.OO#....# +.....##... +OO.#O....O +.O.....O#. +O.#..O.#.# +..O..#O..O +.......O.. +#....###.. +#OO..#.... \ No newline at end of file