Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fun main() {
- val reg = Regex("[\\d]+")
- fun getNumberOfWins(input: String): Int {
- val (winning, held) = input.split(": ")[1].split(" | ")
- val winningSet = reg.findAll(winning).map { it.groupValues[0] }.toList();
- val heldSet = reg.findAll(held).map { it.groupValues[0] }.toList();
- return winningSet.intersect(heldSet).size
- }
- // Using val because I think the alternative makes solution harder to read.
- val zeroAsDouble = 0.toDouble()
- fun part1(input: List<String>): Int {
- val output = input.map { getNumberOfWins(it) } // get match count for each card
- .map { n ->
- if (n > 0) 2.toDouble().pow(n - 1) // score the card if any matches
- else zeroAsDouble // 0 if none
- }
- .sum() // sum
- return output.toInt();
- }
- val input = Path("src/Day04.txt").readLines()
- part1(input).println()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement