Skip to content

Commit 849a80d

Browse files
committed
add 2023/day4 solution
1 parent 147b32f commit 849a80d

File tree

6 files changed

+330
-10
lines changed

6 files changed

+330
-10
lines changed

src/main/kotlin/advent2023/Day04.kt

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package advent2023
2+
3+
import kotlin.math.pow
4+
5+
private const val CARD = "Card"
6+
private const val COLON_SEPARATOR = ":"
7+
8+
class Day04(private val input: List<String>) {
9+
10+
fun solvePart1(): Int {
11+
return input.sumOf { card ->
12+
val matchCount = findMatchCountForCard(card)
13+
2.0.pow(matchCount - 1).toInt()
14+
}
15+
}
16+
17+
fun solvePart2(): Int {
18+
val cardToTotalCount = mutableMapOf<Int, Int>()
19+
20+
val cardToMatchCount = input.map { card ->
21+
val cardNumber = card.substringAfter(CARD)
22+
.substringBefore(COLON_SEPARATOR)
23+
.trim()
24+
.toInt()
25+
.also {
26+
// init `cardToTotalCount` with each card occurring once
27+
cardToTotalCount[it] = 1
28+
}
29+
cardNumber to findMatchCountForCard(card)
30+
}
31+
32+
cardToMatchCount.forEach { (cardNumber, matchCount) ->
33+
repeat(matchCount) {
34+
cardToTotalCount[cardNumber + it + 1] =
35+
checkNotNull(cardToTotalCount[cardNumber + it + 1]) + checkNotNull(cardToTotalCount[cardNumber])
36+
}
37+
}
38+
39+
return cardToTotalCount.map { it.value }.sum()
40+
}
41+
42+
private fun findMatchCountForCard(cardInfo: String): Int {
43+
val numbers = cardInfo.split(":").last().split("|")
44+
45+
val winningNumbers = numbers
46+
.first()
47+
.split(" ")
48+
.filter { it.isNotEmpty() }
49+
.map { it.trim() }
50+
.map { it.toInt() }
51+
val cardsInHand = numbers
52+
.last()
53+
.split(" ")
54+
.filter { it.isNotEmpty() }
55+
.map { it.trim() }
56+
.map { it.toInt() }
57+
58+
var count = 0
59+
cardsInHand.forEach { number ->
60+
if (number in winningNumbers) {
61+
count += 1
62+
}
63+
}
64+
return count
65+
}
66+
}
67+
68+
fun main() {
69+
val input = Resources.resourceAsListOfString("advent2023/day04.txt")
70+
val day04 = Day04(input = input)
71+
println("Part 1 Solution: ${day04.solvePart1()}")
72+
println("Part 2 Solution: ${day04.solvePart2()}")
73+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy