diff --git a/README.md b/README.md index 307ccb3..123f22b 100644 --- a/README.md +++ b/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: 17 / 50 +#### Stars: 18 / 50 | Mon | Tue | Wed | Thu | Fri | Sat | Sun | |:-----------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:| | | | | | 1
:star::star: | 2
:star::star: | 3
:star::star: | -| 4
:star::star: | 5
:star::star: | 6
:star::star: | 7
:star::star: | 8
:star::star: | 9
:star: | 10 | +| 4
:star::star: | 5
:star::star: | 6
:star::star: | 7
:star::star: | 8
:star::star: | 9
:star::star: | 10 | | 11 | 12 | 13 | 14 | 15 | 16 | 17 | | 18 | 19 | 20 | 21 | 22 | 23 | 24 | | 25 | | | | | | | \ No newline at end of file diff --git a/src/day9/Puzzle1.scala b/src/day9/Puzzle1.scala index 81d1f27..d9bcd1d 100644 --- a/src/day9/Puzzle1.scala +++ b/src/day9/Puzzle1.scala @@ -1,9 +1,6 @@ package day9 -import scala.collection.mutable import scala.io.{BufferedSource, Source} -import scala.util.matching.Regex -import scala.util.matching.Regex.Match object Puzzle1 { var series: Array[Series] = Array.empty diff --git a/src/day9/Puzzle2.scala b/src/day9/Puzzle2.scala new file mode 100644 index 0000000..615f673 --- /dev/null +++ b/src/day9/Puzzle2.scala @@ -0,0 +1,38 @@ +package day9 + +import scala.io.{BufferedSource, Source} + +object Puzzle2 { + var series: Array[Series] = Array.empty + + def loadInput(path: String): Unit = { + val source: BufferedSource = Source.fromFile(path) + val lines: Array[String] = source.getLines().toArray + + series = new Array(lines.length) + + for ((line: String, i: Int) <- lines.zipWithIndex) { + series(i) = new Series(line.split(" ").map(_.toInt)) + } + + source.close() + } + + def solve(path: String): Int = { + loadInput(path) + + var solution: Int = 0 + + for (s: Series <- series) { + s.computeDiffs() + solution += s.reverseExtrapolate() + } + + return solution + } + + def main(args: Array[String]): Unit = { + val solution: Int = solve("./res/day9/input1.txt") + println(solution) + } +} diff --git a/src/day9/Series.scala b/src/day9/Series.scala index df546dc..7dbadbb 100644 --- a/src/day9/Series.scala +++ b/src/day9/Series.scala @@ -27,4 +27,13 @@ class Series { } return values(0).last } + + def reverseExtrapolate(): Int = { + val len: Int = values(0).length + values.last(values.last.length-1) = 0 + for (i: Int <- values.length-2 to 0 by -1) { + values(i)(len-i-1) = values(i)(0) - values(i+1)(len-i-2) + } + return values(0).last + } } diff --git a/tests/day9/Puzzle2Test.scala b/tests/day9/Puzzle2Test.scala new file mode 100644 index 0000000..a30e45f --- /dev/null +++ b/tests/day9/Puzzle2Test.scala @@ -0,0 +1,9 @@ +package day9 + +import org.scalatest.funsuite.AnyFunSuite + +class Puzzle2Test extends AnyFunSuite { + test("Puzzle2.solve") { + assert(Puzzle2.solve("tests_res/day9/input1.txt") == 2) + } +}