Advertisement
jdcook3

AoC/2023/Day04Pt1.kt

Feb 1st, 2024
1,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.93 KB | Software | 0 0
  1. fun main() {
  2.  
  3.     val reg = Regex("[\\d]+")
  4.     fun getNumberOfWins(input: String): Int {
  5.         val (winning, held) = input.split(": ")[1].split(" | ")
  6.  
  7.         val winningSet = reg.findAll(winning).map { it.groupValues[0] }.toList();
  8.         val heldSet = reg.findAll(held).map { it.groupValues[0] }.toList();
  9.  
  10.         return winningSet.intersect(heldSet).size
  11.     }
  12.  
  13.     // Using val because I think the alternative makes solution harder to read.
  14.     val zeroAsDouble = 0.toDouble()
  15.  
  16.     fun part1(input: List<String>): Int {
  17.         val output = input.map { getNumberOfWins(it) } // get match count for each card
  18.             .map { n ->
  19.                 if (n > 0) 2.toDouble().pow(n - 1) // score the card if any matches
  20.                 else zeroAsDouble // 0 if none
  21.             }
  22.             .sum() // sum
  23.  
  24.         return output.toInt();
  25.     }
  26.  
  27.     val input = Path("src/Day04.txt").readLines()
  28.     part1(input).println()
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement