-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay1.kt
47 lines (34 loc) · 1.17 KB
/
Day1.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package aoc2019.day01
import java.io.File
import java.io.InputStream
fun computeFuel(mass: Int): Int {
return kotlin.math.floor((mass / 3).toDouble()).toInt() - 2;
}
fun computeTotalFuel(massList: List<Int>): Int {
return massList.sumBy { computeFuel(it) };
}
fun readInputFileToList(path: String): List<Int> {
val inputList = mutableListOf<Int>()
val inputStream: InputStream = File(path).inputStream()
val lineList = mutableListOf<String>()
inputStream.bufferedReader().forEachLine { lineList.add(it) }
lineList.mapTo(inputList) { it.toInt() }
return inputList;
}
fun computeRecursiveFuel(mass: Int): Int {
var recursiveFuel = 0
var computedFuel = computeFuel(mass)
while (computedFuel >= 0) {
recursiveFuel += computedFuel;
computedFuel = computeFuel(computedFuel)
}
return recursiveFuel
}
fun computerRecursiveTotalFuel(massList: List<Int>): Int {
return massList.sumBy { computeRecursiveFuel(it) }
}
fun main() {
val massList = readInputFileToList("src/main/kotlin/adventOfCode/input.txt")
println("Part 1: " + computeTotalFuel(massList))
println("Part 2: " + computerRecursiveTotalFuel(massList))
}