Commit 4d1e2967 authored by David's avatar David

Day 1 complete

parent 55d93adf
package is.kow.adventofcode._2019
import scala.annotation.tailrec
import scala.io.Source
object Puzzle1Main extends App {
//Read in the inputs, and do all the calcs
val source = Source.fromResource("input.txt")
try {
val lines = source.getLines()
val lines = source.getLines().toList
val p1 = new Puzzle1
val totalFuel = lines.foldLeft(0) { (acc, line) =>
......@@ -14,6 +15,13 @@ object Puzzle1Main extends App {
}
println("Total fuel required for part 1: " + totalFuel)
val part2Fuel = lines.foldLeft(0) { (acc, line) =>
acc + p1.recursiveFuel(0, line.toInt)
}
println("total fuel required for part 2: " + part2Fuel)
} finally source.close()
}
......@@ -25,4 +33,14 @@ class Puzzle1 {
}
//recursive fuel calculation function!
@tailrec final def recursiveFuel(acc: Int = 0, input: Int): Int = {
val change = Math.floor(input.toFloat / 3).toInt
if (change <= 2) {
acc
} else {
//do the calculation and add it to accumulator, and recurse
val addFuel = change - 2;
recursiveFuel(acc + addFuel, addFuel)
}
}
}
......@@ -22,4 +22,16 @@ class Puzzle1Spec extends AnyFunSpec {
assert(p1.calculateFuel(100756) == 33583)
}
}
describe("part 2") {
it("returns 2 for 14") {
assert(p1.recursiveFuel(0, 14) == 2)
}
it("returns 966 for 1969") {
assert(p1.recursiveFuel(0, 1969) == 966)
}
it("returns 50346 for 100756") {
assert(p1.recursiveFuel(0, 100756) == 50346)
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment