Skip to content

Commit 17726a8

Browse files
committed
Solved day09 part 1
1 parent be38c34 commit 17726a8

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44

55
// project meta data
66
group 'de.havox_design.aoc2023'
7-
version '0.8.2'
7+
version '0.8.3'
88

99
// Switch to gradle "all" distribution.
1010
wrapper {

day09/src/main/kotlin/de/havox_design/aoc2023/day09/Day09.kt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,38 @@ package de.havox_design.aoc2023.day09
22

33
class Day09(private var filename: String) {
44
fun solvePart1(): Long =
5-
0L
5+
convertInput()
6+
.sumOf { history ->
7+
processOasisReport(history)
8+
}
69

710
fun solvePart2(): Long =
811
0L
912

13+
private fun processOasisReport(oasisHistory: List<Long>): Long {
14+
var value = 0L
15+
var factor = 1
16+
var current = oasisHistory
17+
18+
while (current.any { it != 0L }) {
19+
value += current.last()
20+
21+
current = current.windowed(2).map { (a, b) -> b - a }
22+
}
23+
24+
return value
25+
}
26+
27+
private fun convertInput(): List<List<Long>> =
28+
getResourceAsText(filename)
29+
.map { row ->
30+
row
31+
.split(" ")
32+
.stream()
33+
.map { element -> element.toLong() }
34+
.toList()
35+
}
36+
1037
private fun getResourceAsText(path: String): List<String> =
1138
this.javaClass.classLoader.getResourceAsStream(path)!!.bufferedReader().readLines()
1239
}

day09/src/test/kotlin/de/havox_design/aoc2023/day09/Day09Test.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ class Day09Test {
2727
@JvmStatic
2828
private fun getDataForTestSolvePart1(): Stream<Arguments> =
2929
Stream.of(
30-
Arguments.of("part1sample1.txt", 0L),
31-
Arguments.of("part1sample2.txt", 0L),
32-
Arguments.of("part1sample3.txt", 0L),
33-
Arguments.of("part1sample4.txt", 0L)
30+
Arguments.of("part1sample1.txt", 18L),
31+
Arguments.of("part1sample2.txt", 28L),
32+
Arguments.of("part1sample3.txt", 68L),
33+
Arguments.of("part1sample4.txt", 114L)
3434
)
3535

3636
@JvmStatic

0 commit comments

Comments
 (0)