diff --git a/README.md b/README.md index 123f22b..5872791 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ I will try and do some problems (probably not all of them) in Scala | 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::star: | 10 | +| 4
:star::star: | 5
:star::star: | 6
:star::star: | 7
:star::star: | 8
:star::star: | 9
:star::star: | 10
:star: | | 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/day10/Puzzle1.scala b/src/day10/Puzzle1.scala new file mode 100644 index 0000000..8def782 --- /dev/null +++ b/src/day10/Puzzle1.scala @@ -0,0 +1,74 @@ +package day10 + +import scala.collection.immutable.HashMap +import scala.io.{BufferedSource, Source} + +object Puzzle1 { + var grid: Array[Array[Byte]] = Array.empty + + // bits: NWSE + val TILES: Map[Char, Byte] = HashMap( + '|' -> 10, + '-' -> 5, + 'L' -> 9, + 'J' -> 12, + '7' -> 6, + 'F' -> 3, + '.' -> 0, + 'S' -> 15 + ) + + var height: Int = 0 + var width: Int = 0 + var startX: Int = 0 + var startY: Int = 0 + + def loadInput(path: String): Unit = { + val source: BufferedSource = Source.fromFile(path) + val lines: Array[String] = source.getLines().toArray + + height = lines.length + width = if (height == 0) 0 else lines(0).length + grid = Array.ofDim(height, width) + + for ((line: String, y: Int) <- lines.zipWithIndex) { + for ((c: Char, x: Int) <- line.zipWithIndex) { + grid(y)(x) = TILES(c) + if (c == 'S') { + startX = x + startY = y + } + } + } + + source.close() + } + + def calculateMaxDistance(): Int = { + val walker1: Walker = new Walker(startX, startY) + walker1.walk(grid) + val walker2: Walker = new Walker(startX, startY, walker1.getX(), walker1.getY()) + + while (walker1.getPos() != walker2.getPos()) { + walker2.walk(grid) + if (walker1.getPos() == walker2.getPos()) { + return walker2.getDistance() + } + walker1.walk(grid) + } + return walker1.getDistance() + } + + def solve(path: String): Int = { + loadInput(path) + + val solution: Int = calculateMaxDistance() + + return solution + } + + def main(args: Array[String]): Unit = { + val solution: Int = solve("./res/day10/input1.txt") + println(solution) + } +} diff --git a/src/day10/Walker.scala b/src/day10/Walker.scala new file mode 100644 index 0000000..8da9609 --- /dev/null +++ b/src/day10/Walker.scala @@ -0,0 +1,60 @@ +package day10 + +class Walker { + private var x: Int = 0 + private var y: Int = 0 + private var lastX: Int = -1 + private var lastY: Int = -1 + private var distance: Int = 0 + + val OFFSETS: Array[(Int, Int)] = Array( + (1, 0), (0, 1), (-1, 0), (0, -1) + ) + + def this(startX: Int, startY: Int) = { + this() + x = startX + y = startY + } + + def this(startX: Int, startY: Int, excludeX: Int, excludeY: Int) = { + this(startX, startY) + lastX = excludeX + lastY = excludeY + } + + def getX(): Int = x + def getY(): Int = y + def getPos(): (Int, Int) = (x, y) + def getDistance(): Int = distance + + def walk(grid: Array[Array[Byte]]): Unit = { + val height: Int = grid.length + val width: Int = if (height == 0) 0 else grid(0).length + val (x2: Int, y2: Int) = getNextPos(grid, width, height) + lastX = x + lastY = y + x = x2 + y = y2 + distance += 1 + } + + private def getNextPos(grid: Array[Array[Byte]], width: Int, height: Int): (Int, Int) = { + val curTile: Byte = grid(y)(x) + for (((dx: Int, dy: Int), i: Int) <- OFFSETS.zipWithIndex) { + val x2: Int = x + dx + val y2: Int = y + dy + if (x2 != lastX || y2 != lastY) { + if (0 <= x2 && x2 < width && 0 <= y2 && y2 < height) { + val bit: Byte = (1 << i).toByte + val bit2: Byte = (1 << ((i + 2) % 4)).toByte + if ((curTile & bit) != 0 && (grid(y2)(x2) & bit2) != 0) { + return (x2, y2) + } + } + } + } + + throw new Exception("Dead-end path") + } +} diff --git a/tests/day10/Puzzle1Test.scala b/tests/day10/Puzzle1Test.scala new file mode 100644 index 0000000..5f14d34 --- /dev/null +++ b/tests/day10/Puzzle1Test.scala @@ -0,0 +1,12 @@ +package day10 + +import org.scalatest.funsuite.AnyFunSuite + +class Puzzle1Test extends AnyFunSuite { + test("Puzzle1.solve 1") { + assert(Puzzle1.solve("tests_res/day10/input1.txt") == 4) + } + test("Puzzle1.solve 2") { + assert(Puzzle1.solve("tests_res/day10/input2.txt") == 8) + } +} diff --git a/tests_res/day10/input1.txt b/tests_res/day10/input1.txt new file mode 100644 index 0000000..73b3d66 --- /dev/null +++ b/tests_res/day10/input1.txt @@ -0,0 +1,5 @@ +..... +.S-7. +.|.|. +.L-J. +..... \ No newline at end of file diff --git a/tests_res/day10/input2.txt b/tests_res/day10/input2.txt new file mode 100644 index 0000000..3c00cf2 --- /dev/null +++ b/tests_res/day10/input2.txt @@ -0,0 +1,5 @@ +..F7. +.FJ|. +SJ.L7 +|F--J +LJ... \ No newline at end of file