File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change
1
+ package adventofcode2023
2
+
3
+ import java.io.File
4
+
5
+ object Day09 {
6
+ private fun getPrediction (values : List <Int >, end : Boolean = true): Int {
7
+ return if (values.all { it == 0 }) 0
8
+ else if (end) values.last() + getPrediction(values.zipWithNext { a, b -> b - a })
9
+ else values.first() - getPrediction(values.zipWithNext { a, b -> b - a }, end = false )
10
+ }
11
+
12
+ fun part1 (inputs : List <List <Int >>) = println (inputs.sumOf { getPrediction(it) })
13
+
14
+ fun part2 (inputs : List <List <Int >>) = println (inputs.sumOf { getPrediction(it, end = false ) })
15
+ }
16
+
17
+ fun main () {
18
+ val inputs = File (" resources/adventofcode2023/Day09.txt" )
19
+ .readLines()
20
+ .map { line ->
21
+ line.split(" " ).map { it.toInt() }
22
+ }
23
+
24
+ Day09 .part1(inputs)
25
+ Day09 .part2(inputs)
26
+ }
You can’t perform that action at this time.
0 commit comments