From b4a1ef98f8443eba1b5a3a5491b5a5b7b6861a56 Mon Sep 17 00:00:00 2001 From: Daniel Lin Date: Mon, 4 Dec 2023 01:28:22 -0500 Subject: [PATCH] No need to keep the whole Set --- .../commonMain/kotlin/com/github/ephemient/aoc2023/Day4.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day4.kt b/kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day4.kt index 01ff744e..80f3a88e 100644 --- a/kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day4.kt +++ b/kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day4.kt @@ -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] } }