Advertisement
Guest User

GiantSquid

a guest
Dec 5th, 2021
229
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private fun main() {
  2.     fun part1(input: List<String>): Int {
  3.         val winningNumbers = input.first().split(",").map(String::toInt)
  4.         val boards = mutableListOf<Board>()
  5.         val boardsNumbers = input.subList(2, input.size).flatMap {
  6.             it.split(" ").filterNot(String::isBlank).map(String::toInt)
  7.         }.toList()
  8.  
  9.         val numberOfBoards = boardsNumbers.size / 25
  10.         repeat(numberOfBoards) { boardIndex ->
  11.             val numbers = mutableListOf<Number>()
  12.             repeat(5) { rowIndex ->
  13.                 repeat(5) { columnIndex ->
  14.                     val numberIndex = 25 * boardIndex + 5 * rowIndex + columnIndex
  15.                     numbers.add(Number(boardsNumbers[numberIndex], rowIndex, columnIndex))
  16.                 }
  17.             }
  18.             boards.add(Board(numbers))
  19.         }
  20.  
  21.         for (drawnNumber in winningNumbers) {
  22.             for (board in boards) {
  23.                 for (number in board.numbers) {
  24.                     if (number.value == drawnNumber) {
  25.                         number.drawn = true
  26.                         if (board.hasWon()) {
  27.                             return board.numbers.filter { it.drawn.not() }.sumOf(Number::value) * drawnNumber
  28.                         }
  29.                     }
  30.                 }
  31.  
  32.             }
  33.         }
  34.  
  35.         error("No board has won.")
  36.     }
  37.  
  38.     fun part2(input: List<String>): Int {
  39.         val winningNumbers = input.first().split(",").map(String::toInt)
  40.         val boards = mutableListOf<Board>()
  41.         val boardsNumbers = input.subList(2, input.size).flatMap {
  42.             it.split(" ").filterNot(String::isBlank).map(String::toInt)
  43.         }.toList()
  44.  
  45.         val numberOfBoards = boardsNumbers.size / 25
  46.         repeat(numberOfBoards) { boardIndex ->
  47.             val numbers = mutableListOf<Number>()
  48.             repeat(5) { rowIndex ->
  49.                 repeat(5) { columnIndex ->
  50.                     val numberIndex = 25 * boardIndex + 5 * rowIndex + columnIndex
  51.                     numbers.add(Number(boardsNumbers[numberIndex], rowIndex, columnIndex))
  52.                 }
  53.             }
  54.             boards.add(Board(numbers))
  55.         }
  56.  
  57.         for (drawnNumber in winningNumbers) {
  58.             for (board in boards) {
  59.                 for (number in board.numbers) {
  60.                     if (number.value == drawnNumber) {
  61.                         number.drawn = true
  62.                         if (boards.all(Board::hasWon)) {
  63.                             return board.numbers.filter { it.drawn.not() }.sumOf(Number::value) * drawnNumber
  64.                         }
  65.                     }
  66.                 }
  67.  
  68.             }
  69.         }
  70.  
  71.         error("No board has won.")
  72.     }
  73. }
  74.  
  75. private data class Board(
  76.     val numbers: MutableList<Number>
  77. ) {
  78.     fun hasWon() = hasCompleteRow() || hasCompleteColumn()
  79.     fun hasCompleteRow() = numbers.groupBy { it.x }.any { (_, value) -> value.all(Number::drawn) }
  80.     fun hasCompleteColumn() = numbers.groupBy { it.y }.any { (_, value) -> value.all(Number::drawn) }
  81. }
  82.  
  83. private data class Number(val value: Int, val x: Int, val y: Int, var drawn: Boolean = false)
  84.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement