Skip to content

Commit 76e7042

Browse files
shardulcsjrd
andcommitted
Apply suggestions from @sjrd
Co-authored-by: Sébastien Doeraene <sjrdoeraene@gmail.com>
1 parent eafcc81 commit 76e7042

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

2023/src/day04.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,17 @@ def countWinning(card: String): Int =
2727
.substring(card.indexOf(":") + 1) // discard "Card X:"
2828
.split(" ")
2929
.filterNot(_.isEmpty())
30-
val separatorIndex = numbers.indexOf("|")
31-
val (_winningNumbers, _givenNumbers) = numbers.splitAt(separatorIndex)
32-
val winningNumbers = _winningNumbers.map(Integer.parseInt).toSet
30+
val (winningNumberStrs, givenNumberStrs) = numbers.span(_ != "|")
31+
val winningNumbers = winningNumberStrs.map(_.toInt).toSet
3332
// drop the initial "|"
34-
val givenNumbers = _givenNumbers.drop(1).map(Integer.parseInt).toSet
33+
val givenNumbers = givenNumberStrs.drop(1).map(_.toInt).toSet
3534
winningNumbers.intersect(givenNumbers).size
3635

3736
def part1(input: String): String =
3837
input.linesIterator
3938
.map{ line =>
4039
val winning = countWinning(line)
41-
if winning > 0 then Math.pow(2, winning - 1).toLong else 0
40+
if winning > 0 then Math.pow(2, winning - 1).toInt else 0
4241
}
4342
.sum.toString()
4443
end part1

0 commit comments

Comments
 (0)