|
| 1 | +package de.ronny_h.aoc.year2018.day15 |
| 2 | + |
| 3 | +import de.ronny_h.aoc.AdventOfCode |
| 4 | +import de.ronny_h.aoc.extensions.collections.filterMinBy |
| 5 | +import de.ronny_h.aoc.extensions.graphs.ShortestPath |
| 6 | +import de.ronny_h.aoc.extensions.grids.Coordinates |
| 7 | +import de.ronny_h.aoc.extensions.grids.SimpleCharGrid |
| 8 | +import de.ronny_h.aoc.year2018.day15.CombatArea.PlayerType.Elf |
| 9 | +import de.ronny_h.aoc.year2018.day15.CombatArea.PlayerType.Goblin |
| 10 | +import io.github.oshai.kotlinlogging.KotlinLogging |
| 11 | + |
| 12 | +private val logger = KotlinLogging.logger {} |
| 13 | + |
| 14 | +fun main() = BeverageBandits().run(0, 0) |
| 15 | + |
| 16 | +class BeverageBandits : AdventOfCode<Long>(2018, 15) { |
| 17 | + override fun part1(input: List<String>): Long { |
| 18 | + val combatArea = CombatArea(input) |
| 19 | + while (combatArea.takeOneRound()) { |
| 20 | + // the action takes place in the condition |
| 21 | + } |
| 22 | + return combatArea.outcome() |
| 23 | + } |
| 24 | + |
| 25 | + override fun part2(input: List<String>): Long { |
| 26 | + return 0 |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +class CombatArea(input: List<String>) : SimpleCharGrid(input) { |
| 31 | + private val cavern = '.' |
| 32 | + |
| 33 | + private data class Player( |
| 34 | + val type: PlayerType, |
| 35 | + var position: Coordinates, |
| 36 | + val attackPower: Int = 3, |
| 37 | + var hitPoints: Int = 200 |
| 38 | + ) |
| 39 | + |
| 40 | + private enum class PlayerType(val char: Char) { |
| 41 | + Elf('E'), Goblin('G') |
| 42 | + } |
| 43 | + |
| 44 | + private val units = buildList { |
| 45 | + forEachCoordinates { position, square -> |
| 46 | + when (square) { |
| 47 | + Elf.char -> add(Player(Elf, position)) |
| 48 | + Goblin.char -> add(Player(Goblin, position)) |
| 49 | + } |
| 50 | + }.last() |
| 51 | + }.toMutableList() |
| 52 | + |
| 53 | + private var fullRounds = 0L |
| 54 | + |
| 55 | + /** |
| 56 | + * @return if combat continues |
| 57 | + */ |
| 58 | + fun takeOneRound(): Boolean { |
| 59 | + if (fullRounds % 10 == 0L) logStats() |
| 60 | + units.sortedBy { it.position }.forEach { unit -> |
| 61 | + logger.debug { "unit $unit" } |
| 62 | + val enemyType = if (unit.type == Elf) Goblin else Elf |
| 63 | + val targets = units.filter { it.type == enemyType } |
| 64 | + if (targets.isEmpty()) { |
| 65 | + return false |
| 66 | + } |
| 67 | + |
| 68 | + unit.move(targets) |
| 69 | + unit.attack(targets) |
| 70 | + } |
| 71 | + fullRounds++ |
| 72 | + return true |
| 73 | + } |
| 74 | + |
| 75 | + fun outcome(): Long = fullRounds * units.sumOf { it.hitPoints } |
| 76 | + |
| 77 | + private data class Target(val position: Coordinates, val path: ShortestPath<Coordinates>) |
| 78 | + |
| 79 | + private fun Player.move( |
| 80 | + targets: List<Player> |
| 81 | + ) { |
| 82 | + logger.debug { " moving $this" } |
| 83 | + if (targets.any { it.position in position.neighbours() }) { |
| 84 | + // already in range of a target |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + val targetsWithPaths = targets.flatMap { target -> |
| 89 | + target.position.neighbours() |
| 90 | + .filter { getAt(it) == cavern } |
| 91 | + .flatMap { inRange -> |
| 92 | + shortestPaths(position, inRange) { neighbour -> |
| 93 | + getAt(neighbour) == cavern |
| 94 | + } |
| 95 | + .map { Target(inRange, it) } |
| 96 | + } |
| 97 | + } |
| 98 | + if (targetsWithPaths.isEmpty()) { |
| 99 | + // no path to any target found |
| 100 | + return |
| 101 | + } |
| 102 | + logger.debug { " ${targetsWithPaths.size} paths found" } |
| 103 | + val nearestTargetPath = targetsWithPaths |
| 104 | + .filterMinBy { it.path.distance } |
| 105 | + .filterMinBy { it.position } |
| 106 | + .filterMinBy { it.path.path[1] } |
| 107 | + .first() |
| 108 | + moveTo(nearestTargetPath.path.path[1]) |
| 109 | + } |
| 110 | + |
| 111 | + private fun Player.attack(targets: List<Player>) { |
| 112 | + logger.debug { " attacking with $this" } |
| 113 | + val opponent = position |
| 114 | + .neighbours() |
| 115 | + .mapNotNull { neighbour -> targets.firstOrNull { it.position == neighbour } } |
| 116 | + .filterMinBy { it.hitPoints } |
| 117 | + .minByOrNull { it.position } |
| 118 | + if (opponent == null) return |
| 119 | + |
| 120 | + logger.debug { " hitting $opponent" } |
| 121 | + opponent.hitPoints -= attackPower |
| 122 | + |
| 123 | + if (opponent.hitPoints <= 0) { |
| 124 | + opponent.die() |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private fun Player.moveTo(newPosition: Coordinates) { |
| 129 | + logger.debug { " moving to $newPosition" } |
| 130 | + setAt(position, cavern) |
| 131 | + setAt(newPosition, type.char) |
| 132 | + position = newPosition |
| 133 | + } |
| 134 | + |
| 135 | + private fun Player.die() { |
| 136 | + setAt(position, cavern) |
| 137 | + units.remove(this) |
| 138 | + } |
| 139 | + |
| 140 | + private fun logStats() { |
| 141 | + logger.info { "round $fullRounds: ${elfCount()} elfs, ${goblinCount()} goblins" } |
| 142 | + logger.debug { "\n${toString()}" } |
| 143 | + } |
| 144 | + |
| 145 | + private fun elfCount() = units.count { it.type == Elf } |
| 146 | + private fun goblinCount() = units.count { it.type == Goblin } |
| 147 | +} |
0 commit comments