File tree Expand file tree Collapse file tree 1 file changed +12
-3
lines changed
src/main/scala/advent/solutions Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Original file line number Diff line number Diff line change 1
1
package advent .solutions
2
2
3
+ import scala .annotation .tailrec
4
+
3
5
/** Day 1: The Tyranny of the Rocket Equation
4
6
*
5
7
* @see https://adventofcode.com/2019/day/1
@@ -23,15 +25,22 @@ object Day1 {
23
25
*/
24
26
def sumOfFuel (masses : List [Int ]): Int = {
25
27
// This should use the Day1.fuel function above
26
- ???
28
+ masses.map(fuel).sum
27
29
}
28
30
29
31
/** Calculates the total required to launch a module, including the fuel required to launch the fuel itself
30
32
*
31
33
* @param mass The mass of the module
32
34
* @return The fuel required to launch the module, plus the fuel required to launch that fuel
33
35
*/
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
+ }
35
44
36
45
/** Calculates the sum of the total fuel required to launch each module of a given mass
37
46
*
@@ -40,7 +49,7 @@ object Day1 {
40
49
*/
41
50
def sumOfTotalFuel (masses : List [Int ]): Int = {
42
51
// This should use the Day1.totalFuel function above
43
- ???
52
+ masses.map(totalFuel).sum
44
53
}
45
54
46
55
}
You can’t perform that action at this time.
0 commit comments