Create scala.scala
Solution in Scala
This commit is contained in:
parent
5ed542e0a2
commit
11ec31e720
18
2019/1/scala.scala
Normal file
18
2019/1/scala.scala
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import scala.io.Source
|
||||||
|
val lines = Source.fromFile(getClass.getResource("/input1.txt").getFile).getLines().toList
|
||||||
|
|
||||||
|
// 1
|
||||||
|
val result1 = lines.foldLeft (0) { (acc, i) => acc + (Integer.parseInt(i)/3-2)}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
val result2 = lines.foldLeft (0) { (acc, i) => acc + findRecursiveFuel(Integer.parseInt(i), 0)}
|
||||||
|
println(result2)
|
Loading…
Reference in a new issue