|
| 1 | +package de.ronny_h.aoc.year2025.day10 |
| 2 | + |
| 3 | +import de.ronny_h.aoc.AdventOfCode |
| 4 | +import de.ronny_h.aoc.extensions.memoize |
| 5 | +import de.ronny_h.aoc.year2025.day10.LightState.OFF |
| 6 | +import de.ronny_h.aoc.year2025.day10.LightState.ON |
| 7 | + |
| 8 | +fun main() = Day10().run(481, 0) |
| 9 | + |
| 10 | +class Day10 : AdventOfCode<Int>(2025, 10) { |
| 11 | + override fun part1(input: List<String>): Int { |
| 12 | + var i = 0 |
| 13 | + return input.parseMachineDescriptions().sumOf { |
| 14 | + i++ |
| 15 | + val result = Machine(it).configureIndicatorLights() |
| 16 | + println("machine $i: $result") |
| 17 | + result |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + override fun part2(input: List<String>): Int { |
| 22 | + return 0 |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// [.##.] (3) (1,3) (2) (2,3) (0,2) (0,1) {3,5,4,7} |
| 27 | +fun List<String>.parseMachineDescriptions(): List<MachineDescription> = |
| 28 | + map { line -> |
| 29 | + val indicatorLights = line.substringBefore("] (").drop(1) |
| 30 | + .map { if (it == '#') ON else OFF } |
| 31 | + val wiringSchematics = line.substringAfter("] ").substringBefore(" {") |
| 32 | + .split(" ").map { it.drop(1).dropLast(1).split(",").map(String::toInt) } |
| 33 | + val joltage = line.substringAfter(") {").dropLast(1).split(",").map(String::toInt) |
| 34 | + MachineDescription(indicatorLights, wiringSchematics, joltage) |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | +data class MachineDescription( |
| 39 | + val indicatorLights: List<LightState>, |
| 40 | + val wiringSchematics: List<List<Int>>, |
| 41 | + val joltage: List<Int> |
| 42 | +) |
| 43 | + |
| 44 | +enum class LightState { |
| 45 | + OFF, ON; |
| 46 | + |
| 47 | + fun toggle() = when (this) { |
| 48 | + OFF -> ON |
| 49 | + ON -> OFF |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +class Machine(private val description: MachineDescription, private val maxPressCount: Int = 7) { |
| 54 | + private val indicatorLightsOff = List(description.indicatorLights.size) { OFF } |
| 55 | + private val buttons = description.wiringSchematics |
| 56 | + |
| 57 | + // determine the fewest total presses |
| 58 | + fun configureIndicatorLights(): Int { |
| 59 | + val target = description.indicatorLights |
| 60 | + |
| 61 | + data class RecursiveState(val state: List<LightState>, val buttonsToPress: List<Int>, val pressCount: Int) |
| 62 | + |
| 63 | + lateinit var pressButtons: (RecursiveState) -> Int |
| 64 | + pressButtons = { s -> |
| 65 | + if (s.pressCount == maxPressCount) { |
| 66 | + maxPressCount |
| 67 | + } else { |
| 68 | + val newState = s.state.toMutableList() |
| 69 | + s.buttonsToPress.forEach { newState[it] = newState[it].toggle() } |
| 70 | + if (newState.contentEquals(target)) { |
| 71 | + s.pressCount |
| 72 | + } else { |
| 73 | + buttons.minOf { pressButtons(RecursiveState(newState.toList(), it, s.pressCount + 1)) } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + val pressButtonsMemoized = pressButtons.memoize() |
| 79 | + |
| 80 | + return buttons.minOf { pressButtonsMemoized(RecursiveState(indicatorLightsOff, it, 1)) } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +fun <T> MutableList<T>.contentEquals(other: List<T>): Boolean { |
| 85 | + if (size != other.size) return false |
| 86 | + forEachIndexed { i, element -> if (other[i] != element) return false } |
| 87 | + return true |
| 88 | +} |
0 commit comments