Initial project setup + Day 1
This commit is contained in:
11
src/main/kotlin/com/basdado/adventofcode/Utils.kt
Normal file
11
src/main/kotlin/com/basdado/adventofcode/Utils.kt
Normal file
@@ -0,0 +1,11 @@
|
||||
package com.basdado.adventofcode
|
||||
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Paths
|
||||
import java.util.stream.Stream
|
||||
|
||||
fun lines(resourceFile: String): Stream<String> {
|
||||
return Files.lines(Paths.get(Utils::class.java.getResource(resourceFile).toURI()))
|
||||
}
|
||||
|
||||
class Utils
|
||||
40
src/main/kotlin/com/basdado/adventofcode/day1/Day1.kt
Normal file
40
src/main/kotlin/com/basdado/adventofcode/day1/Day1.kt
Normal file
@@ -0,0 +1,40 @@
|
||||
package com.basdado.adventofcode.day1
|
||||
|
||||
import com.basdado.adventofcode.lines
|
||||
|
||||
const val DAY1_PATH = "/day/1/input.txt"
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
println(Day1.puzzle1())
|
||||
println(Day1.puzzle2())
|
||||
}
|
||||
|
||||
object Day1 {
|
||||
|
||||
fun puzzle1(): Long {
|
||||
return lines(DAY1_PATH)
|
||||
.filter { !it.isBlank() }
|
||||
.mapToLong { it.toLong() }
|
||||
.map { (it / 3) - 2 }
|
||||
.sum()
|
||||
}
|
||||
|
||||
fun puzzle2(): Long {
|
||||
return lines(DAY1_PATH)
|
||||
.filter { !it.isBlank() }
|
||||
.mapToLong { it.toLong() }
|
||||
.map { (it / 3) - 2 }
|
||||
.map { it + calculateFuelFuel(it) }
|
||||
.sum()
|
||||
}
|
||||
|
||||
private fun calculateFuelFuel(initialFuel: Long): Long {
|
||||
var currentFuel = initialFuel
|
||||
var sum = 0L
|
||||
while (currentFuel > 8) {
|
||||
currentFuel = currentFuel / 3 - 2
|
||||
sum += currentFuel
|
||||
}
|
||||
return sum
|
||||
}
|
||||
}
|
||||
100
src/main/resources/day/1/input.txt
Normal file
100
src/main/resources/day/1/input.txt
Normal file
@@ -0,0 +1,100 @@
|
||||
66626
|
||||
95053
|
||||
84365
|
||||
111504
|
||||
97412
|
||||
124986
|
||||
133224
|
||||
60267
|
||||
68096
|
||||
120910
|
||||
105547
|
||||
138575
|
||||
112841
|
||||
113102
|
||||
92387
|
||||
83511
|
||||
62646
|
||||
98974
|
||||
138093
|
||||
59417
|
||||
137854
|
||||
78318
|
||||
143846
|
||||
81514
|
||||
86217
|
||||
98493
|
||||
82056
|
||||
129376
|
||||
61322
|
||||
51045
|
||||
115467
|
||||
106540
|
||||
149439
|
||||
141253
|
||||
65608
|
||||
130480
|
||||
79444
|
||||
80032
|
||||
101908
|
||||
105695
|
||||
145502
|
||||
87538
|
||||
68817
|
||||
128768
|
||||
148784
|
||||
115464
|
||||
147306
|
||||
148666
|
||||
118258
|
||||
82755
|
||||
68422
|
||||
98333
|
||||
105334
|
||||
120963
|
||||
89349
|
||||
78675
|
||||
99151
|
||||
61383
|
||||
81549
|
||||
106544
|
||||
72880
|
||||
88152
|
||||
110879
|
||||
91277
|
||||
123584
|
||||
78430
|
||||
51658
|
||||
126045
|
||||
93833
|
||||
52535
|
||||
130831
|
||||
130920
|
||||
80069
|
||||
140263
|
||||
50943
|
||||
63503
|
||||
116135
|
||||
112686
|
||||
67582
|
||||
83515
|
||||
128736
|
||||
136447
|
||||
69998
|
||||
72472
|
||||
61009
|
||||
136054
|
||||
124675
|
||||
134813
|
||||
149765
|
||||
132135
|
||||
127787
|
||||
148333
|
||||
78020
|
||||
94212
|
||||
81407
|
||||
58994
|
||||
146820
|
||||
66603
|
||||
62761
|
||||
86955
|
||||
Reference in New Issue
Block a user