Skip to content

Commit bf1e80e

Browse files
committed
refactor: lose the last bit of state in day 4
1 parent eb23de0 commit bf1e80e

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

day04.sc

+10-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import common.loadPackets
2-
import scala.annotation.tailrec
32

43
val input = loadPackets(List("day04.txt"))
54
type Board = List[List[Int]]
@@ -22,18 +21,13 @@ def hasBingo(board: Board, turn: Int): Boolean =
2221
def score(board: Board, turn: Int): Int =
2322
board.flatten.filter(!drawn(turn).contains(_)).sum * numbers(turn - 1)
2423

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

Comments
 (0)