day 15 puzzle 1

This commit is contained in:
Louis Heredero 2023-12-15 08:52:58 +01:00
parent 67c609e489
commit 802404ac1f
3 changed files with 52 additions and 0 deletions

42
src/day15/Puzzle1.scala Normal file
View File

@ -0,0 +1,42 @@
package day15
import scala.io.{BufferedSource, Source}
object Puzzle1 {
var values: Array[String] = Array.empty
def loadInput(path: String): Unit = {
val source: BufferedSource = Source.fromFile(path)
val line: String = source.getLines().mkString
values = 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 solve(path: String): Int = {
loadInput(path)
var solution: Int = 0
for (value: String <- values) {
solution += hash(value)
}
return solution
}
def main(args: Array[String]): Unit = {
val solution: Int = solve("./res/day15/input1.txt")
println(solution)
}
}

View File

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

View File

@ -0,0 +1 @@
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7