Golfcoder FAQ LOGIN
Error

Advent of Code Leaderboard 2023 / Day 5

View puzzle on adventofcode.com

Submit solution



Leaderboard

Name Language Tokens Sum Tokens Part 1 Tokens Part 2 Last change
1 Profile imageValentin Slawicek Kotlin 1036 408 628 13 months ago

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, Bash), 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.
  • Your code must be able to process all valid Advent of Code inputs. Golfcoder might reevaluate correctness of your solution with different inputs after your submission.

628 tokens in Kotlin for part 2 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<LongRange>=emptyList()

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

generateSequence(::readLine)
.forEach{line->
when{
line.startsWith("seeds: ")->{
seeds=line.substringAfter("seeds: ")
.split(" ")
.map{it.toLong()}
.chunked(2)
.map{LongRange(it[0],it[0]+it[1]-1)}
}

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]
)
}
}
}

funLongRange.intersect(other:LongRange):LongRange{
returnLongRange(maxOf(this.first,other.first),minOf(this.last,other.last))
}

fungetMapEntries(source:String,range:LongRange):Map<MapEntry,LongRange>{
valrelevantMapEntries=mapEntries.filter{it.source==source}.sortedBy{it.sourceStart}
valtargetMapEntries=relevantMapEntries.toMutableList()
varlastEnd=-1L
relevantMapEntries.forEach{mapEntry->
if(mapEntry.sourceStart!=lastEnd+1){
// Add unmapped gap
targetMapEntries+=relevantMapEntries.first().copy(
sourceStart=lastEnd+1,
destinationStart=lastEnd+1,
rangeLength=mapEntry.sourceStart-lastEnd
)
}
lastEnd=mapEntry.sourceRange.last
}
// Add last unmapped (until Long.MAX_VALUE)
targetMapEntries+=relevantMapEntries.first()
.copy(sourceStart=lastEnd+1,destinationStart=lastEnd+1,rangeLength=Long.MAX_VALUE-lastEnd)

returntargetMapEntries.filter{!it.sourceRange.intersect(range).isEmpty()}
.associateWith{mapEntry->
valoffset=mapEntry.destinationStart-mapEntry.sourceStart
valsourceIntersectedRange=mapEntry.sourceRange.intersect(range)
LongRange(sourceIntersectedRange.first+offset,sourceIntersectedRange.last+offset)
}
}

funresolve(source:String,range:LongRange):List<LongRange>{
valresolvedMapEntries=getMapEntries(source,range)
returnresolvedMapEntries.flatMap{(mapEntry,range)->
if(resolvedMapEntries.keys.first().destination=="location"){
listOf(range)
}else{
resolve(mapEntry.destination,range)
}
}
}

println(seeds.minOf{seed->resolve("seed",seed).minBy{it.first}.first})
}