1
0
Fork 0

day(04): simplify

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2021-12-04 16:24:26 +01:00
parent 47f865d9aa
commit 2d4eb2b19e

View file

@ -2,10 +2,11 @@ package day04
import readInput import readInput
fun main() {
class Bingo(description: List<String>) { class Bingo(description: List<String>) {
private val draws: List<Int> = description[0].split(",").map { it.toInt() }
private var nextDraw: Int = 0 private var nextDraw: Int = 0
private var orderOfWin: MutableList<MutableList<MutableList<Int>>> = mutableListOf()
private val draws: List<Int> = description[0].split(",").map { it.toInt() }
private val boards: List<MutableList<MutableList<Int>>> = description.drop(1).chunked(6) private val boards: List<MutableList<MutableList<Int>>> = description.drop(1).chunked(6)
.map { board -> .map { board ->
board.drop(1) board.drop(1)
@ -18,9 +19,10 @@ fun main() {
} }
.toMutableList() .toMutableList()
} }
private var orderOfWin: MutableList<MutableList<MutableList<Int>>> = mutableListOf()
fun mark(board: MutableList<MutableList<Int>>, number: Int) {
// TODO: Clean up
private fun mark(board: MutableList<MutableList<Int>>, number: Int) {
for (i in board.indices) { for (i in board.indices) {
for (j in board[i].indices) { for (j in board[i].indices) {
if (board[i][j] == number) { if (board[i][j] == number) {
@ -36,90 +38,45 @@ fun main() {
} }
} }
fun isWinningBoard(board: MutableList<MutableList<Int>>): Boolean { private fun isWinningBoard(board: MutableList<MutableList<Int>>): Boolean = board.indices.any { i ->
for (i in board.indices) { board[i].count { it == -1 } == board.size || board.indices.count { board[it][i] == -1 } == board.size
val inRow = board[i].count { it == -1 }
val inColumn = (board.indices).count { board[it][i] == -1 }
if (inRow == board.size || inColumn == board.size) {
return true
}
}
return false
}
fun winningBoardsCount(): Int {
var counter = 0
for (board in boards) {
if (isWinningBoard(board)) {
counter++
}
}
return counter
}
fun boardsCount(): Int {
return boards.size
} }
val winningBoardsCount: Int
get() = boards.count { isWinningBoard(it) }
val boardsCount: Int
get() = boards.size
val hasWinner: Boolean val hasWinner: Boolean
get() { get() = boards.any { isWinningBoard(it) }
for (board in boards) {
if (isWinningBoard(board)) {
return true
}
}
return false
}
fun draw() { fun draw() {
val number = draws[nextDraw++] val number = draws[nextDraw++]
boards.forEach { mark(it, number) }
for (board in boards) {
mark(board, number)
}
} }
fun sumOfUnmarked(): Int { val lastDraw: Int
for (board in boards) { get() {
if (!isWinningBoard(board)) {
continue
}
return board.sumOf { row -> row.filter { it != -1 }.sum() }
}
return -1
}
fun sumOfLast(): Int {
return orderOfWin.last().sumOf { row -> row.filter { it != -1 }.sum() }
}
fun lastDraw(): Int {
check(nextDraw > 0) check(nextDraw > 0)
return draws[nextDraw - 1] return draws[nextDraw - 1]
} }
fun sumOfLast(): Int = orderOfWin.last().sumOf { row -> row.filter { it != -1 }.sum() }
} }
fun part1(input: Bingo): Int { fun part1(input: Bingo): Int {
while (!input.hasWinner) { while (!input.hasWinner) {
input.draw() input.draw()
} }
return input.sumOfLast() * input.lastDraw
return input.sumOfUnmarked() * input.lastDraw()
} }
fun part2(input: Bingo): Int { fun part2(input: Bingo): Int {
while (input.winningBoardsCount() < input.boardsCount()) { while (input.winningBoardsCount < input.boardsCount) {
input.draw() input.draw()
} }
return input.sumOfLast() * input.lastDraw
return input.sumOfLast() * input.lastDraw()
} }
fun main() {
val testInput = Bingo(readInput(4, "test_input")) val testInput = Bingo(readInput(4, "test_input"))
val input = Bingo(readInput(4, "input")) val input = Bingo(readInput(4, "input"))