Here's mine. I'll write it up later. I already had...
# advent-of-code
t
Here's mine. I'll write it up later. I already had a
Point
class that I just reused.
Copy code
class Day20(rawInput: String) {

    private val grid: Map<Point, Int> = parseGrid(rawInput)

    fun solvePart1(): Int =
        grid.maxBy { it.value }!!.value

    fun solvePart2(): Int =
        grid.count { it.value >= 1000 }


    private fun parseGrid(input: String): Map<Point, Int> {
        val grid = mutableMapOf(startingPoint to 0)
        val stack = ArrayDeque<Point>()
        var current = startingPoint
        input.forEach {
            when (it) {
                '(' -> stack.push(current)
                ')' -> current = stack.pop()
                '|' -> current = stack.peek()
                in movementRules -> {
                    // If we are moving to a spot we haven't seen before, we can
                    // record this as a new distance.
                    val nextDistance = grid.getValue(current) + 1
                    current = movementRules.getValue(it).invoke(current)
                    if (current !in grid) grid[current] = nextDistance
                }
            }
        }
        return grid
    }

    companion object {
        private val startingPoint = Point(0, 0)
        private val movementRules = mapOf(
            'N' to Point::up,
            'S' to Point::down,
            'E' to Point::right,
            'W' to Point::left
        )
    }
}
k
That's some very short code you've got there, and I was wondering how that could possibly work. For an input like
E(N|S)E
you get
2
for part one, but I think that needs to be
3
, right?
t
Yeah it doesn't work for all inputs, just the ones I need it to. 😄
k
Cheating!
I've managed to get my program even shorter:
Copy code
fun main(args: Array<String>) {
    println(3574)
    println(8444)
}
t
Fair.
k
But you're right, this is a coding "competition" after all. It's not important that the code works for all inputs simple smile
t
Kinda how I see it. I feel like I would write a different answer if this was for a job interview or something (although to be fair, I'd probably skip that interview if they made me do puzzles). In my day job, I usually handle all the corner cases. AoC - all bets are off. Gets answer == done. 😄