diff --git a/README.md b/README.md index 1b51175..9dc91b6 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: 14 / 50 +#### Stars: 15 / 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 | 9 | 10 | +| 4
:star::star: | 5
:star::star: | 6
:star::star: | 7
:star::star: | 8
:star: | 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/day8/Node.scala b/src/day8/Node.scala new file mode 100644 index 0000000..23f16d2 --- /dev/null +++ b/src/day8/Node.scala @@ -0,0 +1,5 @@ +package day8 + +class Node(val id: String, val nextNodes: Array[String]) { + +} diff --git a/src/day8/Puzzle1.scala b/src/day8/Puzzle1.scala new file mode 100644 index 0000000..a3af51e --- /dev/null +++ b/src/day8/Puzzle1.scala @@ -0,0 +1,47 @@ +package day8 + +import scala.collection.mutable +import scala.io.{BufferedSource, Source} +import scala.util.matching.Regex +import scala.util.matching.Regex.Match + +object Puzzle1 { + var rule: Array[Int] = Array.empty + val nodes: mutable.Map[String, Node] = new mutable.HashMap() + def loadInput(path: String): Unit = { + val source: BufferedSource = Source.fromFile(path) + val lines: String = source.getLines().mkString("\n") + val parts: Array[String] = lines.split("\n\n") + + rule = parts(0).map(c => "LR".indexOf(c)).toArray + val regexp: Regex = new Regex("([A-Z]{3}) = \\(([A-Z]{3}), ([A-Z]{3})\\)") + for (line: String <- parts(1).split("\n")) { + val matches: Match = regexp.findFirstMatchIn(line).get + val id: String = matches.group(1) + val left: String = matches.group(2) + val right: String = matches.group(3) + nodes(id) = new Node(id, Array(left, right)) + } + + source.close() + } + + def solve(path: String): Int = { + loadInput(path) + + var solution: Int = 0 + var id: String = "AAA" + + while (id != "ZZZ") { + id = nodes(id).nextNodes(rule(solution % rule.length)) + solution += 1 + } + + return solution + } + + def main(args: Array[String]): Unit = { + val solution: Int = solve("./res/day8/input1.txt") + println(solution) + } +} diff --git a/tests/day8/Puzzle1Test.scala b/tests/day8/Puzzle1Test.scala new file mode 100644 index 0000000..0321996 --- /dev/null +++ b/tests/day8/Puzzle1Test.scala @@ -0,0 +1,9 @@ +package day8 + +import org.scalatest.funsuite.AnyFunSuite + +class Puzzle1Test extends AnyFunSuite { + test("Puzzle1.solve") { + assert(Puzzle1.solve("tests_res/day8/input1.txt") == 6) + } +} diff --git a/tests_res/day8/input1.txt b/tests_res/day8/input1.txt new file mode 100644 index 0000000..34ffa8a --- /dev/null +++ b/tests_res/day8/input1.txt @@ -0,0 +1,5 @@ +LLR + +AAA = (BBB, BBB) +BBB = (AAA, ZZZ) +ZZZ = (ZZZ, ZZZ) \ No newline at end of file