Skip to content

Commit

Permalink
Day 4 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaeltoledo committed Dec 4, 2023
1 parent 66fa3f4 commit 2670b56
Show file tree
Hide file tree
Showing 4 changed files with 320 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/main/kotlin/net/rafaeltoledo/kotlin/advent/Day04.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package net.rafaeltoledo.kotlin.advent

import kotlin.math.pow

class Day04 {

fun invoke(input: List<String>): Int {
val cards = input.map { it.toScratchCard() }

return cards
.map { card -> card.numbers.filter { card.winningNumbers.contains(it) } }
.sumOf { it.size.calculateCardValue() }
}

fun invoke2(input: List<String>): Int {
val cards = input.map { it.toScratchCard() }

val cardCounter = cards.associate { it.identifier to 1 }.toMutableMap()

cards
.forEachIndexed { index, card ->
val id = index + 1
val multiplier = cardCounter[id] ?: 1
val extras = card.numbers.filter { card.winningNumbers.contains(it) }.count()

for (i in 0 until extras) {
val targetId = id + (i + 1)
cardCounter[targetId]?.let { current ->
cardCounter[targetId] = current + multiplier
}
}
}

return cardCounter.toList().sumOf { it.second }
}
}

private fun Int.calculateCardValue(): Int {
return 2.0.pow((this - 1).toDouble()).toInt()
}

private fun String.toScratchCard(): ScratchCard {
val parts = split(":")

val identifier = parts.first().replace("Card", "").trim().toInt()

val content = parts.last().split("|")
val winningNumbers =
content.first().split(" ").filter { it.isNotEmpty() }.map { it.trim().toInt() }
val numbers = content.last().split(" ").filter { it.isNotEmpty() }.map { it.trim().toInt() }

return ScratchCard(identifier, numbers, winningNumbers)
}

data class ScratchCard(
val identifier: Int,
val numbers: List<Int>,
val winningNumbers: List<Int>,
)
42 changes: 42 additions & 0 deletions src/test/kotlin/net/rafaeltoledo/kotlin/advent/Day04Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.rafaeltoledo.kotlin.advent

import com.google.common.truth.Truth
import com.google.common.truth.Truth.assertThat
import org.junit.jupiter.api.Test

class Day04Test : BaseTest() {

override val day: String = "04"

@Test
fun `Sample data`() {
val input = readInput("sample")

val output = Day04().invoke(input)
assertThat(output).isEqualTo(13)
}

@Test
fun `Sample data increment`() {
val input = readInput("sample")

val output = Day04().invoke2(input)
assertThat(output).isEqualTo(30)
}

@Test
fun `Real data`() {
val input = readInput()

val output = Day04().invoke(input)
assertThat(output).isEqualTo(26914)
}

@Test
fun `Real data increment`() {
val input = readInput()

val output = Day04().invoke2(input)
assertThat(output).isEqualTo(13080971)
}
}
6 changes: 6 additions & 0 deletions src/test/resources/04.sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
Loading

0 comments on commit 2670b56

Please sign in to comment.