Compare commits

..

2 Commits

Author SHA1 Message Date
78b3d77a29 updated README 2023-12-15 09:42:27 +01:00
e6ef68d818 day 15 puzzle 2 2023-12-15 09:42:11 +01:00
5 changed files with 126 additions and 8 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 I will try and do some problems (probably not all of them) in Scala
## Progress: ## Progress:
#### Stars: 26 / 50 #### Stars: 28 / 50
| Mon | Tue | Wed | Thu | Fri | Sat | Sun | | Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|:------------------:|:-----------------:|:------------------:|:------------------:|:-----------------:|:-----------------:|:------------------:| |:------------------:|:-----------------:|:------------------:|:------------------:|:------------------:|:-----------------:|:------------------:|
| | | | | 1<br>:star::star: | 2<br>:star::star: | 3<br>:star::star: | | | | | | 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: | | 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<br>:star::star: | 15 | 16 | 17 | | 11<br>:star::star: | 12 | 13<br>:star::star: | 14<br>:star::star: | 15<br>:star::star: | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 | | 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | | | | | | | | 25 | | | | | | |

33
src/day15/Box.scala Normal file
View File

@ -0,0 +1,33 @@
package day15
import scala.collection.mutable.ArrayBuffer
class Box(val id: Int) {
var lenses: ArrayBuffer[Lens] = new ArrayBuffer()
var lensesLabels: ArrayBuffer[String] = new ArrayBuffer()
def addLens(lens: Lens): Unit = {
if (lensesLabels.contains(lens.label)) {
val i: Int = lensesLabels.indexOf(lens.label)
lenses(i) = lens
} else {
lenses.addOne(lens)
lensesLabels.addOne(lens.label)
}
}
def removeLens(label: String): Unit = {
if (lensesLabels.contains(label)) {
val i: Int = lensesLabels.indexOf(label)
lenses.remove(i)
lensesLabels.remove(i)
}
}
def getFocusingPower(): Long = {
var power: Long = 0
for ((lens: Lens, i: Int) <- lenses.zipWithIndex) {
power += (i+1) * lens.focalLength
}
return power
}
}

5
src/day15/Lens.scala Normal file
View File

@ -0,0 +1,5 @@
package day15
class Lens(val label: String, val focalLength: Int) {
}

71
src/day15/Puzzle2.scala Normal file
View File

@ -0,0 +1,71 @@
package day15
import scala.io.{BufferedSource, Source}
import scala.util.matching.Regex
import scala.util.matching.Regex.Match
object Puzzle2 {
var steps: Array[String] = Array.empty
val boxes: Array[Box] = new Array(256)
val regex: Regex = new Regex("([a-z]+)(-|=\\d)")
def loadInput(path: String): Unit = {
val source: BufferedSource = Source.fromFile(path)
val line: String = source.getLines().mkString
for (i: Int <- 0 until 256) {
boxes(i) = new Box(i)
}
steps = line.split(",")
source.close()
}
def hash(str: String): Int = {
var value: Int = 0
for (c: Char <- str) {
value += c
value *= 17
value %= 256
}
return value
}
def getFocusingPower(): Long = {
var power: Long = 0
for (box: Box <- boxes) {
power += (box.id+1) * box.getFocusingPower()
}
return power
}
def placeLenses(): Unit = {
for (step: String <- steps) {
val m: Match = regex.findFirstMatchIn(step).get
val label: String = m.group(1)
val labelHash: Int = hash(label)
val action: Char = m.group(2)(0)
val box: Box = boxes(labelHash)
if (action == '=') {
val lens: Lens = new Lens(label, m.group(2)(1) - '0')
box.addLens(lens)
} else {
box.removeLens(label)
}
}
}
def solve(path: String): Long = {
loadInput(path)
placeLenses()
val solution: Long = getFocusingPower()
return solution
}
def main(args: Array[String]): Unit = {
val solution: Long = solve("./res/day15/input1.txt")
println(solution)
}
}

View File

@ -0,0 +1,9 @@
package day15
import org.scalatest.funsuite.AnyFunSuite
class Puzzle2Test extends AnyFunSuite {
test("Puzzle2.solve") {
assert(Puzzle2.solve("tests_res/day15/input1.txt") == 145)
}
}