|
| 1 | +package de.ronny_h.aoc.year2017.day07 |
| 2 | + |
| 3 | +import de.ronny_h.aoc.AdventOfCode |
| 4 | + |
| 5 | +fun main() = RecursiveCircus().run("vgzejbd", "1226") |
| 6 | + |
| 7 | +class RecursiveCircus : AdventOfCode<String>(2017, 7) { |
| 8 | + override fun part1(input: List<String>): String = input.parsePrograms().findRootProgram() |
| 9 | + |
| 10 | + private fun List<Program>.findRootProgram(): String { |
| 11 | + val balancingPrograms = filter { it.balancedPrograms.isNotEmpty() } |
| 12 | + val balancedProgramNames = balancingPrograms.flatMap(Program::balancedPrograms) |
| 13 | + return balancingPrograms |
| 14 | + .map(Program::name) |
| 15 | + .filterNot { it in balancedProgramNames } |
| 16 | + .single() |
| 17 | + } |
| 18 | + |
| 19 | + override fun part2(input: List<String>): String { |
| 20 | + val programs = input.parsePrograms() |
| 21 | + val root = programs.findRootProgram() |
| 22 | + val programsByName = programs.associateBy(Program::name) |
| 23 | + |
| 24 | + data class ProgramWeight(val weight: Int, val fixedWrongWeight: Int = 0) |
| 25 | + |
| 26 | + fun weightOf(programName: String): ProgramWeight { |
| 27 | + val program = programsByName.getValue(programName) |
| 28 | + if (program.balancedPrograms.isEmpty()) { |
| 29 | + return ProgramWeight(program.weight) |
| 30 | + } |
| 31 | + val subTowerWeights = program.balancedPrograms.map { weightOf(it) } |
| 32 | + subTowerWeights.find { it.fixedWrongWeight != 0 }?.let { fixed -> |
| 33 | + // we already found the one to fix -> propagate it up the recursive calls |
| 34 | + return ProgramWeight(program.weight + subTowerWeights.sumOf { it.weight }, fixed.fixedWrongWeight) |
| 35 | + } |
| 36 | + |
| 37 | + val firstSubTowerWeight = subTowerWeights.first().weight |
| 38 | + val sameWeightCount = subTowerWeights.count { it.weight == firstSubTowerWeight } |
| 39 | + |
| 40 | + val fixedWrongWeight = when (sameWeightCount) { |
| 41 | + subTowerWeights.size -> { |
| 42 | + // nothing to rebalance |
| 43 | + 0 |
| 44 | + } |
| 45 | + |
| 46 | + 1 -> { |
| 47 | + // the single program with wrong weight is the first sub-tower's root |
| 48 | + val firstSubprogramWeight = programsByName.getValue(program.balancedPrograms.first()).weight |
| 49 | + firstSubprogramWeight + subTowerWeights[1].weight - firstSubTowerWeight |
| 50 | + } |
| 51 | + |
| 52 | + else -> { |
| 53 | + // the single program with wrong weight is one of the others |
| 54 | + val differentWeight = subTowerWeights.single { it.weight != firstSubTowerWeight } |
| 55 | + val differentWeightIndex = subTowerWeights.indexOfFirst { it.weight != firstSubTowerWeight } |
| 56 | + val differentSubprogramWeight = |
| 57 | + programsByName.getValue(program.balancedPrograms[differentWeightIndex]).weight |
| 58 | + |
| 59 | + differentSubprogramWeight + firstSubTowerWeight - differentWeight.weight |
| 60 | + } |
| 61 | + } |
| 62 | + return ProgramWeight(program.weight + subTowerWeights.sumOf { it.weight }, fixedWrongWeight) |
| 63 | + } |
| 64 | + |
| 65 | + return weightOf(root).fixedWrongWeight.toString() |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +data class Program(val name: String, val weight: Int, val balancedPrograms: List<String> = emptyList()) |
| 70 | + |
| 71 | +fun List<String>.parsePrograms(): List<Program> = map { |
| 72 | + val row = it.split(" -> ") |
| 73 | + val (name, weightInBraces) = row.first().split(" ") |
| 74 | + val weight = weightInBraces.substring(1..<weightInBraces.lastIndex).toInt() |
| 75 | + if (row.size == 1) { |
| 76 | + Program(name, weight) |
| 77 | + } else { |
| 78 | + Program(name, weight, row[1].split(", ")) |
| 79 | + } |
| 80 | +} |
0 commit comments