advent-of-code/2019/1/scala.scala

19 lines
492 B
Scala
Raw Normal View History

2019-12-01 12:09:47 +01:00
import scala.io.Source
val lines = Source.fromFile(getClass.getResource("/input1.txt").getFile).getLines().toList
// 1
2019-12-01 12:17:35 +01:00
val result1 = lines.foldLeft (0) { (acc, i) => acc + (i.toInt/3-2)}
2019-12-01 12:09:47 +01:00
println(result1)
// 2
@scala.annotation.tailrec
def findRecursiveFuel(fuel:Int, acc:Int):Int =
{
val nextFuel = (fuel/3)-2
if (nextFuel <= 0) acc
else findRecursiveFuel(nextFuel, nextFuel+acc)
}
2019-12-01 12:17:35 +01:00
val result2 = lines.foldLeft (0) { (acc, i) => acc + findRecursiveFuel(i.toInt, 0)}
2019-12-01 12:09:47 +01:00
println(result2)