-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.kt
47 lines (40 loc) · 1.56 KB
/
day4.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.io.File
fun main() {
val lineList = mutableListOf<String>()
File("src/main/resources/day4.txt").useLines { lines -> lines.forEach { lineList.add(it) } }
val scratchMap = hashMapOf<Int, Int>()
val cardMap = hashMapOf<Int, String>()
lineList.forEach {
val split = it.split(":")
val split2 = split[0].split(" ")
val cardNumber = split2[split2.size - 1].toInt()
cardMap[cardNumber] = split[1]
scratchMap[cardNumber] = 1
}
val cardPoints = mutableListOf<Int>()
cardMap.forEach { card ->
val split = card.value.split("|")
val winNumbers = split[0].split(" ").toMutableList()
val myNumbers = split[1].split(" ").toMutableList()
winNumbers.removeIf { it.toIntOrNull() == null }
myNumbers.removeIf { it.toIntOrNull() == null }
val commonNumbers = winNumbers.intersect(myNumbers.toSet())
if (commonNumbers.isNotEmpty()) {
var points = 1
val cards = scratchMap[card.key]!!
var i = 1
commonNumbers.forEach { value ->
if (value.toInt() != commonNumbers.last().toInt()) {
points *= 2
}
repeat(cards) {
scratchMap[card.key + i] = scratchMap[card.key + i]!! + 1
}
i++
}
cardPoints.add(points)
}
}
println("[Part 1 Solution] Sum of all card points: ${cardPoints.sum()}")
println("[Part 2 Solution] Sum of all scratch cards: ${scratchMap.values.sum()}")
}