1
1
import common .loadPackets
2
- import scala .annotation .tailrec
3
2
4
3
val input = loadPackets(List (" day04.txt" ))
5
4
type Board = List [List [Int ]]
@@ -22,18 +21,13 @@ def hasBingo(board: Board, turn: Int): Boolean =
22
21
def score (board : Board , turn : Int ): Int =
23
22
board.flatten.filter(! drawn(turn).contains(_)).sum * numbers(turn - 1 )
24
23
25
- @ tailrec
26
- def part1 (turn : Int = 0 ): Int =
27
- boards.find(hasBingo(_, turn)) match {
28
- case Some (board) => score(board, turn)
29
- case None => part1(turn + 1 )
30
- }
31
- part1()
32
-
33
- @ tailrec
34
- def part2 (turn : Int = 0 , losers : List [Board ] = boards): Int =
35
- losers.filter(! hasBingo(_, turn)) match {
36
- case Nil => score(losers.head, turn)
37
- case stillNoBingo => part2(turn + 1 , stillNoBingo)
38
- }
39
- part2()
24
+ val part1 = numbers.indices
25
+ .flatMap(turn => boards.find(hasBingo(_, turn)).map(score(_, turn)))
26
+ .head
27
+
28
+ def losers (turn : Int ): List [Board ] = boards.filter(! hasBingo(_, turn))
29
+
30
+ val part2 = numbers.indices
31
+ .find(turn => losers(turn) == Nil )
32
+ .map(turn => score(losers(turn - 1 ).head, turn))
33
+ .get
0 commit comments