-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.kt
85 lines (79 loc) · 2.32 KB
/
Main.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.io.File
fun main() {
val input = File("src/input").useLines { it.toMutableList() }
val bingoNumbers = input.removeAt(0)
val matrixs = ArrayList<Array<IntArray>>(0)
var currentMat = -1
var row = 0
var col = 0
val regex = "\\s+".toRegex()
input.forEach {
val str = it.trim()
if (str != "") {
str.split(regex).forEach {
matrixs[currentMat][row][col] = it.toInt()
col++
}
row++
col = 0
} else {
matrixs.add(Array(5) { IntArray(5) })
currentMat++
row = 0
col = 0
}
}
val resOne = playBingo(bingoNumbers.split(",").map { it.toInt() }, matrixs, true)
val resTwo = playBingo(bingoNumbers.split(",").map { it.toInt() }, matrixs, false)
println(resOne)
println(resTwo)
}
fun checkBingo(matrix: Array<IntArray>): Boolean {
var winH = true
var winV = true
for (i in matrix.indices) {
winH = true
winV = true
for (j in matrix[0].indices) {
if (matrix[i][j] != -1) {
winH = false
}
if (matrix[j][i] != -1) {
winV = false
}
}
if (winH || winV) return true
}
return winH || winV
}
fun calculateRes(matrix: Array<IntArray>, num: Int): Int {
var sum = 0
for (i in matrix.indices) {
for (j in matrix[0].indices) {
if (matrix[i][j] != -1) {
sum += matrix[i][j]
}
}
}
return sum * num
}
fun playBingo(bingoNumbers: List<Int>, matrixs: ArrayList<Array<IntArray>>, stopOnWin: Boolean): Int {
var bingos = IntArray(matrixs.size)
bingoNumbers.forEach { num ->
matrixs.forEachIndexed { index, actualMat ->
for (i in actualMat.indices) {
for (j in actualMat[0].indices) {
if (actualMat[i][j] == num) {
actualMat[i][j] = -1
if (checkBingo(actualMat)) {
bingos[index]++
if (stopOnWin || bingos.none { it == 0 })
return calculateRes(actualMat, num)
}
}
}
}
}
}
return -1
}