1
0

Compare commits

..

1 Commits

Author SHA1 Message Date
b3c99e3607 clean 2021-12-04 12:43:31 +01:00

View File

@ -5,19 +5,24 @@ import be.vandewalleh.aoc.utils.input.Day
@Day @Day
class Day04 : BaseDay() { class Day04 : BaseDay() {
private val numbers by lazy { input.lines.value[0].split(',').map { it.toInt() } } private val parts by lazy { input.text.split("\n\n") }
private val numbers by lazy { parts[0].split(',').map { it.toInt() } }
private val boards by lazy { private val boards by lazy {
input.text.split("\n\n") parts.drop(1)
.drop(1)
.map { it.lines().map { it.trim().split("\\s+".toRegex()).map { it.toInt() } } } .map { it.lines().map { it.trim().split("\\s+".toRegex()).map { it.toInt() } } }
} }
private val results by lazy { boards.map { playBoard(it) } } override fun part1() = boards
.map { playBoard(it, numbers) }
.minByOrNull { it.first }!!
.second
override fun part1() = results.minByOrNull { it.first }!!.second override fun part2() = boards
override fun part2() = results.maxByOrNull { it.first }!!.second .map { playBoard(it, numbers) }
.maxByOrNull { it.first }!!
.second
private fun playBoard(board: List<List<Int>>): Pair<Int, Int> { private fun playBoard(board: List<List<Int>>, numbers: List<Int>): Pair<Int, Int> {
for (round in numbers.indices) { for (round in numbers.indices) {
val usedNumbers = numbers.take(round) val usedNumbers = numbers.take(round)
for (line in board + board[0].indices.map { col -> board.map { it[col] } }) { for (line in board + board[0].indices.map { col -> board.map { it[col] } }) {