day 13 puzzle 2
This commit is contained in:
parent
0fb66d1bc7
commit
6b134b55c5
16
README.md
16
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<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: | 14 | 15 | 16 | 17 |
|
||||
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
|
||||
| 25 | | | | | | |
|
||||
#### Stars: 24 / 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 |
|
||||
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
|
||||
| 25 | | | | | | |
|
94
src/day13/Puzzle2.scala
Normal file
94
src/day13/Puzzle2.scala
Normal file
@ -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)
|
||||
}
|
||||
}
|
9
tests/day13/Puzzle2Test.scala
Normal file
9
tests/day13/Puzzle2Test.scala
Normal file
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user