day 13 puzzle 1

This commit is contained in:
Louis Heredero 2023-12-13 07:50:11 +01:00
parent c693cd9259
commit 0fb66d1bc7
4 changed files with 118 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: 22 / 50
#### Stars: 23 / 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 | 14 | 15 | 16 | 17 |
| 11<br>:star::star: | 12 | 13<br>:star: | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | | | | | | |

92
src/day13/Puzzle1.scala Normal file
View File

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

View File

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

View File

@ -0,0 +1,15 @@
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#