day 9 puzzle 2
This commit is contained in:
@ -1,9 +1,6 @@
|
||||
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
|
||||
|
38
src/day9/Puzzle2.scala
Normal file
38
src/day9/Puzzle2.scala
Normal file
@ -0,0 +1,38 @@
|
||||
package day9
|
||||
|
||||
import scala.io.{BufferedSource, Source}
|
||||
|
||||
object Puzzle2 {
|
||||
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.reverseExtrapolate()
|
||||
}
|
||||
|
||||
return solution
|
||||
}
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
val solution: Int = solve("./res/day9/input1.txt")
|
||||
println(solution)
|
||||
}
|
||||
}
|
@ -27,4 +27,13 @@ class Series {
|
||||
}
|
||||
return values(0).last
|
||||
}
|
||||
|
||||
def reverseExtrapolate(): 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)(0) - values(i+1)(len-i-2)
|
||||
}
|
||||
return values(0).last
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user