Skip to content

Commit 33518c4

Browse files
committed
Add 2023 Day 09 solutions
1 parent 708f245 commit 33518c4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

adventofcode2023/Day09.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy