Add files via upload

This commit is contained in:
kamoshi 2019-12-06 22:26:44 +01:00 committed by GitHub
parent 483f962bc0
commit 9c102b0246
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 0 deletions

View file

@ -0,0 +1,23 @@
package day01
import kamlib.{Reader, Wrapper}
class Day1 {
/** Find fuel needed */
def solveP1(input: List[Int]): Int =
input.foldLeft (0) {(acc, i) => acc + (i/3-2)}
/** Find fuel needed and recursive fuel */
def solveP2(input: List[Int]): Int =
{
@scala.annotation.tailrec
def findRecursiveFuel(fuel: Int, acc: Int): Int =
{
if (fuel <= 0) acc
else findRecursiveFuel((fuel/3)-2, fuel+acc)
}
input.foldLeft (0) { (acc, i) => acc + findRecursiveFuel((i/3)-2, 0)}
}
}

View file

@ -0,0 +1,19 @@
package day01
import kamlib.{Reader, Wrapper}
object Main {
/** MAIN */
def main(args: Array[String]): Unit =
{
val input: List[Int] = Reader.readList("/input1.txt").map(_.toInt)
val solution: Day1 = new Day1
println("Part 1")
Wrapper(solution.solveP1(input)).print()
println("Part 2")
Wrapper(solution.solveP2(input)).print()
}
}

View 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 + (i.toInt/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(i.toInt, 0)}
println(result2)