-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay01.kt
34 lines (30 loc) · 864 Bytes
/
Day01.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
import java.io.BufferedReader
import java.io.File
fun main() {
println(Day01().part1())
println(Day01().part2())
}
class Day01 {
fun part1(): Int {
val reader: BufferedReader = File("files/day1.txt").bufferedReader()
return reader.lines()
.map { x: String -> x.toInt() / 3 - 2 }
.reduce { acc: Int, x: Int -> x + acc }
.get()
}
fun part2(): Int {
val reader: BufferedReader = File("files/day1.txt").bufferedReader()
return reader.lines()
.map { x: String -> x.toInt() }
.map(::computeFuel)
.reduce { acc: Int, x: Int -> x + acc }
.get()
}
private fun computeFuel(x: Int): Int {
if(x / 3 - 2 <= 0){
return 0
}
val value = x / 3 - 2
return value + computeFuel(value)
}
}