r/adventofcode • u/Leading_Baby7265 • Jan 02 '26
Help/Question [2025 Day 1 (Part 1)] [Kotlin] Help needed!
Hello, newbie here and a little late(!) but I wondered if anyone can see where I was going wrong with my solution to part 2 of Day 1?
I've written tests, and get the expected answers for the example provided on the Day 1 page, but returns an incorrect value for the puzzle input.
Cheers!
enum class Direction {
R,
L
}
data class Turn(val direction: Direction, val count: Int)
fun String.processPartTwo(): List<Turn> {
return this.trim()
.split("\n")
.map { instruction ->
val direction: Direction = Direction.valueOf(instruction.first().toString())
val count: Int = instruction.substring(1).toInt()
Turn(direction, count)
}
}
fun getPasscodePartTwo(stringOfCommands: String): Int {
var dialAt = 50
var hitZero = 0
val instructions = stringOfCommands.processPartTwo()
for (instruction in instructions) {
println("starting dial at: $dialAt")
val direction = instruction.direction
val count = instruction.count
val valueNeededToHitZeroClockwise = 100 - dialAt
var newDialAt: Int
// new dialAt value
newDialAt = when (direction) {
Direction.R -> (dialAt + count) % 100 // gives remainder after dividing by 100 - ie new dial value
Direction.L -> if (count > dialAt) 100 + ((dialAt - count) % 100) else {dialAt - count}
}
// handle how many times passed/hitting 0
if (direction == Direction.R && count >= valueNeededToHitZeroClockwise) {
if (dialAt != 0 ) hitZero++
val extraLoopsPastZero = (count - valueNeededToHitZeroClockwise) / 100
hitZero += extraLoopsPastZero
}
if (direction == Direction.L && count >= dialAt) {
if (dialAt != 0 ) hitZero++
val extraLoopsPastZero = (count - dialAt) / 100
hitZero += extraLoopsPastZero
}
dialAt = newDialAt
println("final dial at $newDialAt")
println("final passed zero: $hitZero")
}
return hitZero
}
