Skip to content

Commit

Permalink
2023 - Day04 - part 2 - changed implementation to just looping an arr…
Browse files Browse the repository at this point in the history
…ay once. performance almost the same though.
  • Loading branch information
fmmr committed Dec 4, 2023
1 parent 28774c4 commit f8af711
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/main/kotlin/no/rodland/advent_2023/Day04.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ class Day04(val input: List<String>) : Day<Long, Long, List<Day04.Game>> {
}

override fun partTwo(): Long {
return partTwoWithArray()
}

private fun partTwoWithArray(): Long {
val ar = parsed.toTypedArray()
ar.forEach { game ->
val endIndex = game.id + game.numberWinning
(game.id..< endIndex).forEach { i ->
ar[i] = ar[i].copy(numCards = ar[i].numCards + game.numCards)
}
}
return ar.sumOf { it.numCards }
}

@Suppress("unused")
private fun partTwoWithFold(): Long {
val end = parsed.fold(parsed) { acc: List<Game>, game: Game ->
val done = acc.subList(0, game.id)
val mustFix = acc.subList(game.id, acc.size)
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/no/rodland/advent_2023/Day04Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal class Day04Test {
private val resultOne = 25571L
private val resultTwo = 8805731L

val test = defaultTestSuiteParseOnInit(Day04(data04), Day04(test04), resultTestOne, resultOne, resultTestTwo, resultTwo, numTestPart2 = 6)
val test = defaultTestSuiteParseOnInit(Day04(data04), Day04(test04), resultTestOne, resultOne, resultTestTwo, resultTwo, numTestPart2 = 20)

@Nested
inner class Init {
Expand Down

0 comments on commit f8af711

Please sign in to comment.