|
| 1 | +package de.ronny_h.aoc.year2017.day08 |
| 2 | + |
| 3 | +import de.ronny_h.aoc.AdventOfCode |
| 4 | + |
| 5 | +fun main() = IHeardYouLikeRegisters().run(8022, 9819) |
| 6 | + |
| 7 | +class IHeardYouLikeRegisters : AdventOfCode<Int>(2017, 8) { |
| 8 | + override fun part1(input: List<String>): Int = executeInstructions(input).maxFinalValue |
| 9 | + override fun part2(input: List<String>): Int = executeInstructions(input).maxProcessValue |
| 10 | + |
| 11 | + data class Result(val maxFinalValue: Int, val maxProcessValue: Int) |
| 12 | + |
| 13 | + private fun executeInstructions(input: List<String>): Result { |
| 14 | + val registers = mutableMapOf<String, Int>() |
| 15 | + var maxProcessValue = 0 |
| 16 | + input.parseInstructions().forEach { |
| 17 | + if (it.condition.appliesTo(registers.getOrDefault(it.conditionRegister, 0), it.conditionValue)) { |
| 18 | + val value = it.operation.applyTo(registers.getOrDefault(it.register, 0), it.amount) |
| 19 | + if (value > maxProcessValue) { |
| 20 | + maxProcessValue = value |
| 21 | + } |
| 22 | + registers[it.register] = value |
| 23 | + } |
| 24 | + } |
| 25 | + return Result(registers.values.max(), maxProcessValue) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +enum class Operation(private val symbol: String) { |
| 30 | + Increase("inc") { |
| 31 | + override fun applyTo(first: Int, second: Int): Int = first + second |
| 32 | + }, |
| 33 | + Decrease("dec") { |
| 34 | + override fun applyTo(first: Int, second: Int): Int = first - second |
| 35 | + }; |
| 36 | + |
| 37 | + abstract fun applyTo(first: Int, second: Int): Int |
| 38 | + |
| 39 | + companion object { |
| 40 | + private val bySymbol = Operation.entries.associateBy { it.symbol } |
| 41 | + |
| 42 | + fun of(symbol: String) = bySymbol.getValue(symbol) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +enum class Condition(private val symbol: String) { |
| 47 | + Equal("==") { |
| 48 | + override fun appliesTo(first: Int, second: Int): Boolean = first == second |
| 49 | + }, |
| 50 | + NotEqual("!=") { |
| 51 | + override fun appliesTo(first: Int, second: Int): Boolean = first != second |
| 52 | + }, |
| 53 | + LessThan("<") { |
| 54 | + override fun appliesTo(first: Int, second: Int): Boolean = first < second |
| 55 | + }, |
| 56 | + LessThanOrEqual("<=") { |
| 57 | + override fun appliesTo(first: Int, second: Int): Boolean = first <= second |
| 58 | + }, |
| 59 | + GreaterThan(">") { |
| 60 | + override fun appliesTo(first: Int, second: Int): Boolean = first > second |
| 61 | + }, |
| 62 | + GreaterThanOrEqual(">=") { |
| 63 | + override fun appliesTo(first: Int, second: Int): Boolean = first >= second |
| 64 | + }; |
| 65 | + |
| 66 | + abstract fun appliesTo(first: Int, second: Int): Boolean |
| 67 | + |
| 68 | + companion object { |
| 69 | + private val bySymbol = Condition.entries.associateBy { it.symbol } |
| 70 | + |
| 71 | + fun of(symbol: String) = bySymbol.getValue(symbol) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +data class Instruction( |
| 76 | + val register: String, |
| 77 | + val operation: Operation, |
| 78 | + val amount: Int, |
| 79 | + val conditionRegister: String, |
| 80 | + val condition: Condition, |
| 81 | + val conditionValue: Int |
| 82 | +) |
| 83 | + |
| 84 | +fun List<String>.parseInstructions(): List<Instruction> = map { |
| 85 | + val parameters = it.split(" ") |
| 86 | + Instruction( |
| 87 | + register = parameters[0], |
| 88 | + operation = Operation.of(parameters[1]), |
| 89 | + amount = parameters[2].toInt(), |
| 90 | + conditionRegister = parameters[4], |
| 91 | + condition = Condition.of(parameters[5]), |
| 92 | + conditionValue = parameters[6].toInt(), |
| 93 | + ) |
| 94 | +} |
0 commit comments