From 11ec31e72074bce4ae41bee4afc3faa78bb91ebe Mon Sep 17 00:00:00 2001 From: kamoshi <18511281+kamoshi@users.noreply.github.com> Date: Sun, 1 Dec 2019 12:09:47 +0100 Subject: [PATCH] Create scala.scala Solution in Scala --- 2019/1/scala.scala | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 2019/1/scala.scala diff --git a/2019/1/scala.scala b/2019/1/scala.scala new file mode 100644 index 0000000..0f62bda --- /dev/null +++ b/2019/1/scala.scala @@ -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)