Compare commits

..

No commits in common. "78b3d77a29fc8d2cac46474639a26f015ee77470" and "802404ac1ff1c7aec52c23875c9448cb0734769e" have entirely different histories.

5 changed files with 8 additions and 126 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: 28 / 50 #### Stars: 26 / 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<br>:star::star: | 16 | 17 | | 11<br>:star::star: | 12 | 13<br>:star::star: | 14<br>:star::star: | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 | | 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | | | | | | | | 25 | | | | | | |

View File

@ -1,33 +0,0 @@
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
}
}

View File

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

View File

@ -1,71 +0,0 @@
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

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