Skip to content

Commit 9a6785b

Browse files
authored
#7 resubmit day1 solution (#8)
1 parent e8d9650 commit 9a6785b

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/main/scala/advent/solutions/Day1.scala

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package advent.solutions
22

3+
import scala.annotation.tailrec
4+
35
/** Day 1: The Tyranny of the Rocket Equation
46
*
57
* @see https://adventofcode.com/2019/day/1
@@ -23,15 +25,22 @@ object Day1 {
2325
*/
2426
def sumOfFuel(masses: List[Int]): Int = {
2527
// This should use the Day1.fuel function above
26-
???
28+
masses.map(fuel).sum
2729
}
2830

2931
/** Calculates the total required to launch a module, including the fuel required to launch the fuel itself
3032
*
3133
* @param mass The mass of the module
3234
* @return The fuel required to launch the module, plus the fuel required to launch that fuel
3335
*/
34-
def totalFuel(mass: Int): Int = ???
36+
def totalFuel(mass: Int): Int = {
37+
@tailrec
38+
def go(currentFuel: Int, accum: Int): Int =
39+
if (currentFuel < 0) accum else go(fuel(currentFuel), accum + currentFuel)
40+
41+
go(fuel(mass), 0)
42+
43+
}
3544

3645
/** Calculates the sum of the total fuel required to launch each module of a given mass
3746
*
@@ -40,7 +49,7 @@ object Day1 {
4049
*/
4150
def sumOfTotalFuel(masses: List[Int]): Int = {
4251
// This should use the Day1.totalFuel function above
43-
???
52+
masses.map(totalFuel).sum
4453
}
4554

4655
}

0 commit comments

Comments
 (0)