day 14 puzzle 1

This commit is contained in:
Louis Heredero 2023-12-14 07:37:10 +01:00
parent 6b134b55c5
commit d36d403402
4 changed files with 89 additions and 2 deletions

View File

@ -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<br>:star::star: | 2<br>:star::star: | 3<br>:star::star: |
| 4<br>:star::star: | 5<br>:star::star: | 6<br>:star::star: | 7<br>:star::star: | 8<br>:star::star: | 9<br>:star::star: | 10<br>:star::star: |
| 11<br>:star::star: | 12 | 13<br>:star::star: | 14 | 15 | 16 | 17 |
| 11<br>:star::star: | 12 | 13<br>:star::star: | 14<br>:star: | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | | | | | | |

68
src/day14/Puzzle1.scala Normal file
View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -0,0 +1,10 @@
O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....