-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66fa3f4
commit 2670b56
Showing
4 changed files
with
320 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
src/test/kotlin/net/rafaeltoledo/kotlin/advent/Day04Test.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.