day 9 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: 16 / 50 | ||||
| #### Stars: 17 / 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<br>:star::star: |         9         |        10         | | ||||
| | 4<br>:star::star: | 5<br>:star::star: | 6<br>:star::star: | 7<br>:star::star: | 8<br>:star::star: |    9<br>:star:    |        10         | | ||||
| |        11         |        12         |        13         |        14         |        15         |        16         |        17         | | ||||
| |        18         |        19         |        20         |        21         |        22         |        23         |        24         | | ||||
| |        25         |                   |                   |                   |                   |                   |                   | | ||||
							
								
								
									
										41
									
								
								src/day9/Puzzle1.scala
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								src/day9/Puzzle1.scala
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,41 @@ | ||||
| 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 | ||||
|  | ||||
|   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.extrapolate() | ||||
|     } | ||||
|  | ||||
|     return solution | ||||
|   } | ||||
|  | ||||
|   def main(args: Array[String]): Unit = { | ||||
|     val solution: Int = solve("./res/day9/input1.txt") | ||||
|     println(solution) | ||||
|   } | ||||
| } | ||||
							
								
								
									
										30
									
								
								src/day9/Series.scala
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								src/day9/Series.scala
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | ||||
| package day9 | ||||
|  | ||||
| import scala.collection.mutable.ArrayBuffer | ||||
|  | ||||
| class Series { | ||||
|   private val values: ArrayBuffer[Array[Int]] = new ArrayBuffer() | ||||
|   def this(initialValues: Array[Int]) = { | ||||
|     this() | ||||
|     values.addOne(initialValues.concat(Array(0))) | ||||
|   } | ||||
|  | ||||
|   def computeDiffs(): Unit = { | ||||
|     while (!(values.last.dropRight(1).distinct.length == 1 && values.last(0) == 0)) { | ||||
|       val diffs: Array[Int] = new Array(values.last.length-1) | ||||
|       for (i: Int <- 0 until diffs.length-1) { | ||||
|         diffs(i) = values.last(i+1) - values.last(i) | ||||
|       } | ||||
|       values.addOne(diffs) | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   def extrapolate(): 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)(len-i-2) + values(i+1)(len-i-2) | ||||
|     } | ||||
|     return values(0).last | ||||
|   } | ||||
| } | ||||
							
								
								
									
										9
									
								
								tests/day9/Puzzle1Test.scala
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								tests/day9/Puzzle1Test.scala
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| package day9 | ||||
|  | ||||
| import org.scalatest.funsuite.AnyFunSuite | ||||
|  | ||||
| class Puzzle1Test extends AnyFunSuite { | ||||
|   test("Puzzle1.solve") { | ||||
|     assert(Puzzle1.solve("tests_res/day9/input1.txt") == 114) | ||||
|   } | ||||
| } | ||||
							
								
								
									
										3
									
								
								tests_res/day9/input1.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								tests_res/day9/input1.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | ||||
| 0 3 6 9 12 15 | ||||
| 1 3 6 10 15 21 | ||||
| 10 13 16 21 30 45 | ||||
		Reference in New Issue
	
	Block a user