diff --git a/README.md b/README.md index 0a1e47b..1c3dee7 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: 8 / 50 -| Mon | Tue | Wed | Thu | Fri | Sat | Sun | -|:-----------------:|:---:|:---:|:---:|:-----------------:|:-----------------:|:-----------------:| -| | | | | 1
:star::star: | 2
:star::star: | 3
:star::star: | -| 4
:star::star: | 5 | 6 | 7 | 8 | 9 | 10 | -| 11 | 12 | 13 | 14 | 15 | 16 | 17 | -| 18 | 19 | 20 | 21 | 22 | 23 | 24 | -| 25 | | | | | | | \ No newline at end of file +#### Stars: 9 / 50 +| Mon | Tue | Wed | Thu | Fri | Sat | Sun | +|:-----------------:|:-----------:|:---:|:---:|:-----------------:|:-----------------:|:-----------------:| +| | | | | 1
:star::star: | 2
:star::star: | 3
:star::star: | +| 4
:star::star: | 5
:star: | 6 | 7 | 8 | 9 | 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/day5/Puzzle1.scala b/src/day5/Puzzle1.scala new file mode 100644 index 0000000..7fefa8c --- /dev/null +++ b/src/day5/Puzzle1.scala @@ -0,0 +1,83 @@ +package day5 + +import scala.io.{BufferedSource, Source} + +object Puzzle1 { + case class Range(startSrc: Long, startDst: Long, length: Long) + var forwardingTables: Array[Array[Range]] = Array.empty + var seeds: Array[Long] = Array.empty + def loadInput(path: String): Unit = { + val source: BufferedSource = Source.fromFile(path) + + val almanac: String = source.getLines().mkString("\n") + val tables: Array[String] = almanac.split("\n\n") + + forwardingTables = new Array(tables.length - 1) + seeds = tables(0).substring(7).split(" ").map(s => s.toLong) + + for (i: Int <- 1 until tables.length) { + val lines: Array[String] = tables(i).split(":\n")(1).split("\n") + val table: Array[Range] = new Array(lines.length) + for ((line: String, j: Int) <- lines.zipWithIndex) { + val values: Array[Long] = line.split(" ").map(v => v.toLong) + table(j) = Range(values(1), values(0), values(2)) + } + forwardingTables(i-1) = table + } + + source.close() + } + + def sortTables(): Unit = { + for (table: Array[Range] <- forwardingTables) { + var changed: Boolean = false + do { + changed = false + for (i: Int <- 0 until table.length-1) { + if (table(i).startSrc >= table(i + 1).startSrc + table(i + 1).length) { + changed = true + val range: Range = table(i) + table(i) = table(i + 1) + table(i + 1) = range + } + } + } while (changed) + } + } + def map(seed: Long): Long = { + var value: Long = seed + for (i: Int <- forwardingTables.indices) { + value = forward(value, i) + } + return value + } + + def forward(value: Long, tableI: Int): Long = { + for (range: Range <- forwardingTables(tableI)) { + if (value >= range.startSrc) { + if (value < range.startSrc + range.length) { + return value - range.startSrc + range.startDst + } + } else { + return value + } + } + return value + } + def solve(path: String): Long = { + loadInput(path) + sortTables() + var smallest: Long = -1 + for (seed: Long <- seeds) { + val location: Long = map(seed) + if (smallest == -1 || location < smallest) { + smallest = location + } + } + return smallest + } + def main(args: Array[String]): Unit = { + val solution: Long = solve("res/day5/input1.txt") + println(solution) + } +} diff --git a/tests/day5/Puzzle1Test.scala b/tests/day5/Puzzle1Test.scala new file mode 100644 index 0000000..1216485 --- /dev/null +++ b/tests/day5/Puzzle1Test.scala @@ -0,0 +1,9 @@ +package day5 + +import org.scalatest.funsuite.AnyFunSuite + +class Puzzle1Test extends AnyFunSuite { + test("Puzzle1.solve") { + assert(Puzzle1.solve("tests_res/day5/input1.txt") == 35) + } +} diff --git a/tests_res/day5/input1.txt b/tests_res/day5/input1.txt new file mode 100644 index 0000000..bd902a4 --- /dev/null +++ b/tests_res/day5/input1.txt @@ -0,0 +1,33 @@ +seeds: 79 14 55 13 + +seed-to-soil map: +50 98 2 +52 50 48 + +soil-to-fertilizer map: +0 15 37 +37 52 2 +39 0 15 + +fertilizer-to-water map: +49 53 8 +0 11 42 +42 0 7 +57 7 4 + +water-to-light map: +88 18 7 +18 25 70 + +light-to-temperature map: +45 77 23 +81 45 19 +68 64 13 + +temperature-to-humidity map: +0 69 1 +1 0 69 + +humidity-to-location map: +60 56 37 +56 93 4 \ No newline at end of file