From f7ea52c685ad765ae2ba46ba8ef17a34c0ee49b3 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sun, 5 Dec 2021 11:23:15 +0100 Subject: [PATCH] day(04): clean up and remove todo Signed-off-by: Matej Focko --- src/day04/Day04.kt | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/day04/Day04.kt b/src/day04/Day04.kt index 3eda0f8..9d66b63 100644 --- a/src/day04/Day04.kt +++ b/src/day04/Day04.kt @@ -20,22 +20,19 @@ class Bingo(description: List) { .toMutableList() } - - // TODO: Clean up private fun mark(board: MutableList>, number: Int) { - for (i in board.indices) { - for (j in board[i].indices) { - if (board[i][j] == number) { - val before = isWinningBoard(board) - board[i][j] = -1 - val after = isWinningBoard(board) + board.indices + .flatMap { i -> board.indices.map { j -> Pair(i, j) } } + .filter { (i, j) -> board[i][j] == number } + .forEach { (i, j) -> + val before = isWinningBoard(board) + board[i][j] = -1 + val after = isWinningBoard(board) - if (before != after) { - orderOfWin.add(board) - } + if (before != after) { + orderOfWin.add(board) } } - } } private fun isWinningBoard(board: MutableList>): Boolean = board.indices.any { i -> @@ -59,6 +56,7 @@ class Bingo(description: List) { check(nextDraw > 0) return draws[nextDraw - 1] } + fun sumOfLast(): Int = orderOfWin.last().sumOf { row -> row.filter { it != -1 }.sum() } }