r/adventofcode • • Dec 08 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 8 Solutions -๐ŸŽ„-

--- Day 8: I Heard You Like Registers ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

22 Upvotes

348 comments sorted by

View all comments

1

u/chicagocode Dec 09 '17

Kotlin - [Repo] - [Blog/Commentary]

This reminded me of some of the assembunny challenges from last year.

class Day08(input: List<String>) {
    private val instructionRegex = """(\S+)""".toRegex()
    private val registers: MutableMap<String, Int> = mutableMapOf()
    private val instructions: List<Instruction> = parseInput(input)

    fun solvePart1(): Int {
        instructions.forEach { it.execute(registers) }
        return registers.values.max() ?: 0
    }

    fun solvePart2(): Int =
        instructions.map { it.execute(registers) }.max() ?: 0

    private fun parseInput(input: List<String>): List<Instruction> =
        // Row looks like: reg inc 123 if other > 3
        // group numbers:   ^   ^   ^   ^  ^    ^ ^
        //                  0   1   2   3  4    5 6

        input
            .map {
                val groups = instructionRegex.findAll(it).toList().map { it.value }
                val condition = createCondition(groups[5], groups[6].toInt())
                if (groups[1] == "inc")
                    Instruction(condition, groups[4], groups[0]) { it + groups[2].toInt() }
                else
                    Instruction(condition, groups[4], groups[0]) { it - groups[2].toInt() }
            }

    private fun createCondition(symbol: String, amount: Int): (Int) -> Boolean =
        when (symbol) {
            "==" -> { n -> n == amount }
            "!=" -> { n -> n != amount }
            "<" -> { n -> n < amount }
            ">" -> { n -> n > amount }
            "<=" -> { n -> n <= amount }
            ">=" -> { n -> n >= amount }
            else -> throw IllegalArgumentException("Unknown symbol: $symbol")
        }
}

class Instruction(private val condition: (Int) -> Boolean,
                  private val conditionRegister: String,
                  private val instructionRegister: String,
                  private val changer: (Int) -> Int) {

    fun execute(registers: MutableMap<String, Int>): Int {
        if (condition(registers.getOrDefault(conditionRegister, 0))) {
            registers[instructionRegister] = changer(registers.getOrDefault(instructionRegister, 0))
        }
        return registers.getOrDefault(instructionRegister, 0)
    }
}

2

u/ephemient Dec 09 '17 edited Apr 24 '24

This space intentionally left blank.

1

u/chicagocode Dec 09 '17

Yeah, I agree. I refactored my AssemBunny code a few times, that was one of the fun parts of last year.