Skip to content

Commit

Permalink
No need to keep the whole Set
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 4, 2023
1 parent fbf4c3d commit b4a1ef9
Showing 1 changed file with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ class Day4(input: String) {
private val cards = input.lines().mapNotNull { line ->
val (left, right) = line.substringAfter(':').split('|', limit = 2).takeIf { it.size == 2 }
?: return@mapNotNull null
left.trim().split(WHITESPACE).toSet() intersect right.trim().split(WHITESPACE).toSet()
(left.trim().split(WHITESPACE).toSet() intersect right.trim().split(WHITESPACE).toSet()).size
}

fun part1(): Int = cards.sumOf { 1 shl it.size shr 1 }
fun part1(): Int = cards.sumOf { 1 shl it shr 1 }

fun part2(): Int = IntArray(cards.size) { 1 }.also { counts ->
for ((i, card) in cards.withIndex()) {
for (j in i + 1..(i + card.size).coerceAtMost(counts.lastIndex)) {
for (j in i + 1..(i + card).coerceAtMost(counts.lastIndex)) {
counts[j] += counts[i]
}
}
Expand Down

0 comments on commit b4a1ef9

Please sign in to comment.