|
| 1 | +package me.peckb.aoc._2025.calendar.day01 |
| 2 | + |
| 3 | +import me.peckb.aoc._2025.calendar.day01.Day01.Direction.LEFT |
| 4 | +import me.peckb.aoc._2025.calendar.day01.Day01.Direction.RIGHT |
| 5 | +import javax.inject.Inject |
| 6 | +import me.peckb.aoc.generators.InputGenerator.InputGeneratorFactory |
| 7 | +import kotlin.sequences.forEach |
| 8 | + |
| 9 | +class Day01 @Inject constructor(private val generatorFactory: InputGeneratorFactory) { |
| 10 | + companion object { |
| 11 | + const val DIAL_NUMBERS = 100 |
| 12 | + const val START_LOCATION = 50 |
| 13 | + } |
| 14 | + |
| 15 | + fun partOne(filename: String) = generatorFactory.forFile(filename).readAs(::rotation) { rotations -> |
| 16 | + var dialLocation = START_LOCATION |
| 17 | + var countAtZeroAfterMovement = 0 |
| 18 | + |
| 19 | + rotations.forEach { rotation -> |
| 20 | + when (rotation.direction) { |
| 21 | + LEFT -> dialLocation -= rotation.count |
| 22 | + RIGHT -> dialLocation += rotation.count |
| 23 | + } |
| 24 | + |
| 25 | + while (dialLocation !in 0 until DIAL_NUMBERS) { |
| 26 | + if (dialLocation < 0) { |
| 27 | + dialLocation += DIAL_NUMBERS |
| 28 | + } else { // if (dialLocation >= DIAL_NUMBERS) |
| 29 | + dialLocation -= DIAL_NUMBERS |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + if (dialLocation == 0) { countAtZeroAfterMovement++ } |
| 34 | + } |
| 35 | + |
| 36 | + countAtZeroAfterMovement |
| 37 | + } |
| 38 | + |
| 39 | + fun partTwo(filename: String) = generatorFactory.forFile(filename).readAs(::rotation) { rotations -> |
| 40 | + var dialLocation = START_LOCATION |
| 41 | + var countAtZeroAfterMovement = 0 |
| 42 | + |
| 43 | + rotations.forEach { rotation -> |
| 44 | + val fullLoops = rotation.count / DIAL_NUMBERS |
| 45 | + val remainingTurns = rotation.count % DIAL_NUMBERS |
| 46 | + |
| 47 | + // always increment a full loop |
| 48 | + countAtZeroAfterMovement += fullLoops |
| 49 | + |
| 50 | + val dialLocationBeforeChanging = dialLocation |
| 51 | + if (remainingTurns != 0) { |
| 52 | + when (rotation.direction) { |
| 53 | + RIGHT -> { |
| 54 | + dialLocation += remainingTurns |
| 55 | + // if we pass over 99, increment again |
| 56 | + if (dialLocation >= DIAL_NUMBERS) { |
| 57 | + dialLocation -= DIAL_NUMBERS |
| 58 | + countAtZeroAfterMovement++ |
| 59 | + } |
| 60 | + } |
| 61 | + LEFT -> { |
| 62 | + dialLocation -= remainingTurns |
| 63 | + // if we go under 0 ... maybe increment |
| 64 | + if (dialLocation <= 0) { |
| 65 | + // going under zero but starting at zero shouldn't double count |
| 66 | + if (dialLocationBeforeChanging != 0) { countAtZeroAfterMovement++ } |
| 67 | + // but either way we're under zero and need to fix our dial |
| 68 | + if (dialLocation < 0) { dialLocation += DIAL_NUMBERS } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + countAtZeroAfterMovement |
| 76 | + } |
| 77 | + |
| 78 | + private fun rotation(line: String) : Rotation { |
| 79 | + val direction = when(val dc = line[0]) { |
| 80 | + 'L' -> LEFT |
| 81 | + 'R' -> RIGHT |
| 82 | + else -> throw IllegalStateException("Unknown direction $dc") |
| 83 | + } |
| 84 | + val count = line.drop(1).toInt() |
| 85 | + |
| 86 | + return Rotation(direction, count) |
| 87 | + } |
| 88 | + |
| 89 | + enum class Direction(val d: String) { |
| 90 | + LEFT("L"), RIGHT("R"); |
| 91 | + |
| 92 | + override fun toString(): String = d |
| 93 | + } |
| 94 | + |
| 95 | + data class Rotation(val direction: Direction, val count: Int) { |
| 96 | + override fun toString(): String = "$direction$count" |
| 97 | + } |
| 98 | +} |
0 commit comments