File tree Expand file tree Collapse file tree 3 files changed +17
-9
lines changed
main/kotlin/de/havox_design/aoc2023/day09
test/kotlin/de/havox_design/aoc2023/day09 Expand file tree Collapse file tree 3 files changed +17
-9
lines changed Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ plugins {
44
55// project meta data
66group ' de.havox_design.aoc2023'
7- version ' 0.8.4 '
7+ version ' 0.8.5 '
88
99// Switch to gradle "all" distribution.
1010wrapper {
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments