Skip to content

Commit 640ac7f

Browse files
committed
Solved day09 part 2
1 parent aa725b2 commit 640ac7f

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
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.4'
7+
version '0.8.5'
88

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

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,27 @@ class Day09(private var filename: String) {
44
fun solvePart1(): Long =
55
convertInput()
66
.sumOf { history ->
7-
processOasisReport(history)
8-
}
7+
processOasisReport(history, false)
8+
}
99

1010
fun solvePart2(): Long =
11-
0L
11+
convertInput()
12+
.sumOf { history ->
13+
processOasisReport(history, true)
14+
}
1215

13-
private fun processOasisReport(oasisHistory: List<Long>): Long {
16+
private fun processOasisReport(oasisHistory: List<Long>, calculateLeftHistory: Boolean): Long {
1417
var value = 0L
1518
var factor = 1
1619
var current = oasisHistory
1720

1821
while (current.any { it != 0L }) {
19-
value += current.last()
22+
if (calculateLeftHistory) {
23+
value += current.first() * factor
24+
factor *= -1
25+
} else {
26+
value += current.last()
27+
}
2028

2129
current = current.windowed(2).map { (a, b) -> b - a }
2230
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ class Day09Test {
3636
@JvmStatic
3737
private fun getDataForTestSolvePart2(): Stream<Arguments> =
3838
Stream.of(
39-
Arguments.of("sample1.txt", 0L),
39+
Arguments.of("sample1.txt", -3L),
4040
Arguments.of("sample2.txt", 0L),
41-
Arguments.of("sample3.txt", 0L),
42-
Arguments.of("sample4.txt", 0L)
41+
Arguments.of("sample3.txt", 5L),
42+
Arguments.of("sample4.txt", 2L)
4343
)
4444
}
4545
}

0 commit comments

Comments
 (0)