From 802404ac1ff1c7aec52c23875c9448cb0734769e Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Fri, 15 Dec 2023 08:52:58 +0100 Subject: [PATCH] day 15 puzzle 1 --- src/day15/Puzzle1.scala | 42 +++++++++++++++++++++++++++++++++++ tests/day15/Puzzle1Test.scala | 9 ++++++++ tests_res/day15/input1.txt | 1 + 3 files changed, 52 insertions(+) create mode 100644 src/day15/Puzzle1.scala create mode 100644 tests/day15/Puzzle1Test.scala create mode 100644 tests_res/day15/input1.txt diff --git a/src/day15/Puzzle1.scala b/src/day15/Puzzle1.scala new file mode 100644 index 0000000..fc5a220 --- /dev/null +++ b/src/day15/Puzzle1.scala @@ -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) + } +} diff --git a/tests/day15/Puzzle1Test.scala b/tests/day15/Puzzle1Test.scala new file mode 100644 index 0000000..e48ca5f --- /dev/null +++ b/tests/day15/Puzzle1Test.scala @@ -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) + } +} diff --git a/tests_res/day15/input1.txt b/tests_res/day15/input1.txt new file mode 100644 index 0000000..62f7ed0 --- /dev/null +++ b/tests_res/day15/input1.txt @@ -0,0 +1 @@ +rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7 \ No newline at end of file