clean
This commit is contained in:
parent
97334d34eb
commit
5c94aed258
@ -5,46 +5,27 @@ import be.vandewalleh.aoc.utils.input.Day
|
||||
|
||||
@Day
|
||||
class Day04 : BaseDay() {
|
||||
private val parts by lazy { input.text.split("\n\n") }
|
||||
private val numbers by lazy { parts[0].split(',').map { it.toInt() } }
|
||||
private val numbers by lazy { input.lines.value[0].split(',').map { it.toInt() } }
|
||||
private val boards by lazy {
|
||||
parts.drop(1)
|
||||
input.text.split("\n\n")
|
||||
.drop(1)
|
||||
.map { it.lines().map { it.trim().split("\\s+".toRegex()).map { it.toInt() } } }
|
||||
}
|
||||
|
||||
override fun part1() = boards
|
||||
.map { playBoard(it, numbers) }
|
||||
.minByOrNull { it.first }!!
|
||||
.second
|
||||
private val results by lazy { boards.map { playBoard(it) } }
|
||||
|
||||
override fun part2() = boards
|
||||
.map { playBoard(it, numbers) }
|
||||
.maxByOrNull { it.first }!!
|
||||
.second
|
||||
override fun part1() = results.minByOrNull { it.first }!!.second
|
||||
override fun part2() = results.maxByOrNull { it.first }!!.second
|
||||
|
||||
private fun playBoard(board: List<List<Int>>, numbers: List<Int>): Pair<Int, Int> {
|
||||
private fun playBoard(board: List<List<Int>>): Pair<Int, Int> {
|
||||
for (round in numbers.indices) {
|
||||
val usedNumbers = numbers.take(round)
|
||||
var winning = false
|
||||
for (line in board) {
|
||||
for (line in board + board[0].indices.map { col -> board.map { it[col] } }) {
|
||||
if (usedNumbers.containsAll(line)) {
|
||||
winning = true
|
||||
break
|
||||
val unmarkedNumbers = board.flatten().toMutableSet().also { it.removeAll(usedNumbers) }
|
||||
return round to unmarkedNumbers.sum() * usedNumbers.last()
|
||||
}
|
||||
}
|
||||
if (!winning) {
|
||||
for (i in board[0].indices) {
|
||||
val column = board.map { it[i] }
|
||||
if (usedNumbers.containsAll(column)) {
|
||||
winning = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if (winning) {
|
||||
val unmarkedNumbers = board.flatten().toMutableSet().also { it.removeAll(usedNumbers) }
|
||||
return round to unmarkedNumbers.sum() * usedNumbers.last()
|
||||
}
|
||||
}
|
||||
error("No wins")
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user