-
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
Showing
4 changed files
with
283 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,41 @@ | ||
package io.plagov.advent.aoc2023 | ||
|
||
import kotlin.math.pow | ||
|
||
class Day04 { | ||
|
||
fun partOne(input: List<String>): Int { | ||
val cards = input.map { cardString -> parseCardString(cardString) } | ||
return cards.sumOf { card -> getResultForCard(card) } | ||
} | ||
|
||
private fun getResultForCard(card: Card): Int { | ||
val count = card.yourNumbers.count { it in card.winningNumbers } | ||
return when (count) { | ||
1 -> 1 | ||
2 -> 2 | ||
else -> 2.toDouble().pow((count - 1)).toInt() | ||
} | ||
} | ||
|
||
private fun parseCardString(cardString: String): Card { | ||
val winningNumbers = cardString | ||
.substringAfter(": ").substringBefore(" |") | ||
.trim() | ||
.split("""\s+""".toRegex()) | ||
.map { it.toInt() } | ||
|
||
val yourNumbers = cardString | ||
.substringAfter("| ") | ||
.trim() | ||
.split("""\s+""".toRegex()) | ||
.map { it.toInt() } | ||
|
||
return Card(winningNumbers, yourNumbers) | ||
} | ||
} | ||
|
||
data class Card( | ||
val winningNumbers: List<Int>, | ||
val yourNumbers: List<Int> | ||
) |
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,24 @@ | ||
package io.plagov.advent.aoc2023 | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
import util.readInputFile | ||
|
||
class Day04Test { | ||
|
||
private val sampleInput = readInputFile("2023/day04-sample.txt") | ||
|
||
private val taskInput = readInputFile("2023/day04.txt") | ||
|
||
private val day04 = Day04() | ||
|
||
@Test | ||
fun solveFirstForSampleInput() { | ||
assertEquals(13, day04.partOne(sampleInput)) | ||
} | ||
|
||
@Test | ||
fun solveFirstForTaskInput() { | ||
assertEquals(123, day04.partOne(taskInput)) | ||
} | ||
} |
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.