Golfcoder FAQ LOGIN
Error

Advent of Code Leaderboard 2023 / Day 4

View puzzle on adventofcode.com

Submit solution



Rules
  • You're welcome to participate alone or in a team.
  • You may submit multiple solutions and explore different programming languages.
  • Stick to the standard library of your language, no further dependencies/libraries, except the ones which OneCompiler provides (e.g. NumPy for Python).
  • Ensure your code aligns to the template (Python, Rust, Go, Kotlin, JavaScript, C#, TypeScript, C++, Java, C, Swift, Scala, Ruby), reading the puzzle input from stdin (terminated with end-of-file), and printing the solution to stdout.
  • Please refrain from making network requests, reading data from files, or storing data in variable/function/class names for reflection.

Leaderboard

Name Language Tokens Sum Tokens Part 1 Tokens Part 2 Last change
1 Profile imageVisible-Bag4062 Python 180 57 123 8 months ago
2 Profile imageLiquidFun Python 188 71 117 8 months ago
3 Profile imageMustafa Furkan Kaptan Python 202 70 132 8 months ago
4 Profile imageValentin Slawicek Kotlin 523 233 290 8 months ago

290 tokens in Kotlin for part 2 by Valentin Slawicek

Download solution

dataclassCard(valcardNumber:Int,valwinningNumbers:Set<Int>,valnumbersYouHave:Set<Int>)

funmain(){
// Parse input
valcards=generateSequence(::readLine)
.map{line->
valcardNumber=line.substringBefore(":").substringAfterLast(" ").toInt()
valwinningNumbers=line
.substringAfter(":")
.substringBefore("|")
.split(" ")
.mapNotNull{number->number.trim().takeIf{it.isNotEmpty()}?.toInt()}
valnumbersYouHave=line
.substringAfter("|")
.split(" ")
.mapNotNull{number->number.trim().takeIf{it.isNotEmpty()}?.toInt()}
Card(cardNumber,winningNumbers.toSet(),numbersYouHave.toSet())
}

// Calculate score
valcardsToMatches:Map<Card,Int>=cards.associateWith{it.winningNumbers.intersect(it.numbersYouHave).count()}
valcardCount:MutableMap<Int,Int>=cardsToMatches.keys.map{it.cardNumber}.associateWith{1}.toMutableMap()// Card number to matches
cardsToMatches.forEach{(card,matches)->
(card.cardNumber+1..card.cardNumber+matches).forEach{nextCardNumber->
cardCount[nextCardNumber]=cardCount[nextCardNumber]!!+cardCount[card.cardNumber]!!
}
}
println(cardCount.values.sum())
}