Golfcoder FAQ LOGIN
Error

Advent of Code Leaderboard 2023 / Day 5

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 imageValentin Slawicek Kotlin 1036 408 628 7 months ago

408 tokens in Kotlin for part 1 by Valentin Slawicek

Download solution

dataclassMapEntry(
valsource:String,
valdestination:String,
valsourceStart:Long,
valdestinationStart:Long,
valrangeLength:Long
){
valsourceRange=LongRange(sourceStart,sourceStart+rangeLength-1)
}

funmain(){
valmapEntries=mutableListOf<MapEntry>()

varseeds:List<Long>=emptyList()

varcurrentMap=""to""// Start to end

generateSequence(::readLine)
.forEach{line->
when{
line.startsWith("seeds: ")->{
seeds=line.substringAfter("seeds: ").split(" ").map{it.toLong()}
}
line.endsWith(" map:")->{
currentMap=line.substringBefore("-")toline.substringAfter("-to-").substringBefore(" ")
}
line.isEmpty()->Unit// Skip
else->{
valmapEntryString=line.split(" ").map{it.toLong()}
mapEntries+=MapEntry(
source=currentMap.first,
destination=currentMap.second,
destinationStart=mapEntryString[0],
sourceStart=mapEntryString[1],
rangeLength=mapEntryString[2]
)
}
}
}

fungetMapEntry(source:String,number:Long):MapEntry{
// O(n), but we don't care for such small list lengths
returnmapEntries.singleOrNull{it.source==source&&it.sourceStart<=number&&it.sourceStart+it.rangeLength>number}// Mapped
?:mapEntries.first{it.source==source}.copy(sourceStart=number,destinationStart=number,rangeLength=1)// Unmapped
}

funresolve(source:String,number:Long):Long{
valmapEntry=getMapEntry(source,number)
valdestinationNumber=mapEntry.destinationStart+number-mapEntry.sourceStart
returnif(mapEntry.destination=="location"){
destinationNumber
}else{
resolve(mapEntry.destination,destinationNumber)
}
}

println(seeds.minOf{resolve("seed",it)})
}