day 8 puzzle 1
This commit is contained in:
		| @@ -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<br>:star::star: | 2<br>:star::star: | 3<br>:star::star: | | ||||
| | 4<br>:star::star: | 5<br>:star::star: | 6<br>:star::star: | 7<br>:star::star: |         8         |         9         |        10         | | ||||
| | 4<br>:star::star: | 5<br>:star::star: | 6<br>:star::star: | 7<br>:star::star: |    8<br>:star:    |         9         |        10         | | ||||
| |        11         |        12         |        13         |        14         |        15         |        16         |        17         | | ||||
| |        18         |        19         |        20         |        21         |        22         |        23         |        24         | | ||||
| |        25         |                   |                   |                   |                   |                   |                   | | ||||
							
								
								
									
										5
									
								
								src/day8/Node.scala
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								src/day8/Node.scala
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,5 @@ | ||||
| package day8 | ||||
|  | ||||
| class Node(val id: String, val nextNodes: Array[String]) { | ||||
|  | ||||
| } | ||||
							
								
								
									
										47
									
								
								src/day8/Puzzle1.scala
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								src/day8/Puzzle1.scala
									
									
									
									
									
										Normal file
									
								
							| @@ -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) | ||||
|   } | ||||
| } | ||||
							
								
								
									
										9
									
								
								tests/day8/Puzzle1Test.scala
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								tests/day8/Puzzle1Test.scala
									
									
									
									
									
										Normal file
									
								
							| @@ -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) | ||||
|   } | ||||
| } | ||||
							
								
								
									
										5
									
								
								tests_res/day8/input1.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								tests_res/day8/input1.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,5 @@ | ||||
| LLR | ||||
|  | ||||
| AAA = (BBB, BBB) | ||||
| BBB = (AAA, ZZZ) | ||||
| ZZZ = (ZZZ, ZZZ) | ||||
		Reference in New Issue
	
	Block a user