-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day04.kt
32 lines (25 loc) · 978 Bytes
/
Day04.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
private fun findWinner(numbers: List<Int>, boards: List<List<List<Int>>>, position: Int): Int {
val marked = mutableSetOf<Int>()
val won = mutableSetOf<Int>()
for (number in numbers) {
marked.add(number)
for ((i, board) in boards.withIndex()) {
if (i in won) continue
val isRowMarked = board.any { row -> row.all { it in marked } }
val isColumnMarked = board.indices.any { j -> board.all { it[j] in marked } }
if (isRowMarked || isColumnMarked) {
won.add(i)
if (won.size == position) {
return number * board.flatten().filter { it !in marked }.sum()
}
}
}
}
throw IllegalArgumentException("Invalid input")
}
fun main() {
val numbers = readln().toInts()
val boards = mapBlocks { it.toInts() }
println(findWinner(numbers, boards, 1))
println(findWinner(numbers, boards, boards.size))
}