Golfcoder FAQ LOGIN
Error

Advent of Code Leaderboard 2023 / Day 3

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 imageLiquidFun Python 555 275 280 6 months ago
2 Profile imageValentin Slawicek Kotlin 1198 576 622 7 months ago
3 Profile imageVisible-Bag4062 C++ 10473 473 - 7 months ago

622 tokens in Kotlin for part 2 by Valentin Slawicek

Download solution

sealedclassCharacter(valx:Int,valy:Int)
classSymbol(x:Int,y:Int,valchar:Char,valattachedPartNumbers:MutableList<Int>=mutableListOf()):
Character(x,y)

classEmpty(x:Int,y:Int):Character(x,y)
classDigit(x:Int,y:Int,valdigit:Int,varattachedSymbol:Symbol=null):Character(x,y)

funmain(){
// Parse schematic
valschematic:List<List<Character>>=generateSequence(::readLine)
.toList()
.mapIndexed{x,line->
line.mapIndexed{y,char->
when{
char=='.'->Empty(x,y)
char.isDigit()->Digit(x,y,char.digitToInt())
else->Symbol(x,y,char)
}
}
}

fungetAdjacentCharacters(x:Int,y:Int):List<Character>{
returnlistOfNotNull(
schematic.getOrNull(x-1)?.getOrNull(y-1),
schematic.getOrNull(x-1)?.getOrNull(y),
schematic.getOrNull(x-1)?.getOrNull(y+1),
schematic.getOrNull(x)?.getOrNull(y-1),
schematic.getOrNull(x)?.getOrNull(y+1),
schematic.getOrNull(x+1)?.getOrNull(y-1),
schematic.getOrNull(x+1)?.getOrNull(y),
schematic.getOrNull(x+1)?.getOrNull(y+1),
)
}

funflagDigits(character:Character,symbol:Symbol){
getAdjacentCharacters(character.x,character.y)
.filterIsInstance<Digit>()
.filter{it.attachedSymbol==null}
.forEach{digit->
digit.attachedSymbol=symbol
flagDigits(digit,symbol)
}
}

// Traverse schematic to flag digits with its attached symbol
schematic.forEachIndexed{x,charactersLine->
charactersLine.forEachIndexed{y,character->
if(characterisSymbol){
flagDigits(character,character)
}
}
}

// Traverse schematic to get the final digit sum which have an attached symbol
vardigitSumWithAttachedSymbol=0
schematic.forEachIndexed{x,charactersLine->
valcurrentDigits=mutableListOf<Digit>()

charactersLine.forEachIndexed{y,character->
if(characterisDigit&&character.attachedSymbol!=null){
currentDigits+=character
}else{
// Treat each other character as separator
if(currentDigits.isNotEmpty()){
valpartNumber=currentDigits.joinToString(""){it.digit.toString()}.toInt()
currentDigits.first().attachedSymbol!!.attachedPartNumbers+=partNumber
digitSumWithAttachedSymbol+=partNumber
currentDigits.clear()
}
}
}
// New line is also a separator
if(currentDigits.isNotEmpty()){
valpartNumber=currentDigits.joinToString(""){it.digit.toString()}.toInt()
currentDigits.first().attachedSymbol!!.attachedPartNumbers+=partNumber
digitSumWithAttachedSymbol+=partNumber
currentDigits.clear()
}
}

println(schematic.sumOf{line->
line.sumOf{character->
(characteras?Symbol)?.attachedPartNumbers?.takeIf{it.count()==2}?.let{it[0]*it[1]}?:0
}
})
}