AoC Day 23: Experimental Emergency Teleportation

twitter logo github logo ・1 min read

Part of "Advent of Code" series

Day 23! Two days left until Christmas! We have now officially graduated from 2D coordinates, and we've got nanobots flying around in three dimensions! These nanobots are going to help us save a reindeer friend of Santa's, somehow. But first, we'll need to figure out which nanobots can talk to which.

Good luck!

twitter logo DISCUSS (2)
markdown guide
 

Yesterday's was tough and if I'm honest I'm getting tired. So it was nice to find a straightforward part 1 at least.

Model

Simple stuff, we've done this many times already.

data class Pos(val x: Int, val y: Int, val z: Int)

data class Nanobot(val pos: Pos, val radius: Int)

typealias Manhattan = Int

operator fun Pos.minus(other: Pos): Manhattan = 
    Math.abs(x - other.x) + Math.abs(y - other.y) + Math.abs(z - other.z)

Parser

I've used parser combinators on nearly 2 out of 3 days this year, and I'm not going to stop now! At this point I'm writing them without requiring much reference to the documentation.

fun parse(input: String): List<Nanobot> {
    val integer = or(
        string("-").next(INTEGER).map { s -> -s.toInt() },
        INTEGER.map(String::toInt)
    )

    val pos = sequence(string("pos=<").next(integer),
                       string(",").next(integer),
                       string(",").next(integer).followedBy(string(">")),
                       ::Pos)

    val nanobot = sequence(pos, string(", r=").next(integer), ::Nanobot)

    return nanobot.sepBy(WHITESPACES).parse(input.trim())
}

Part 1

Easy stuff.

fun part1(nanobots: List<Nanobot>): Int {
    val strongest = nanobots.maxBy { n -> n.radius }!!
    return nanobots.count { n -> (strongest.pos - n.pos) <= strongest.radius }
}

Part 2

Oh wow, another doozy. I'll get back to you on this one!

 

Yeah you’ve definitely stuck with it. Nice job! 👍🏻

Classic DEV Post from Jan 12

An Engineer’s Rite of Passage

Let's share our stories about the first time we took down production and any advice we have that helped us get through it!

Ryan Palo profile image
Ryan is a mechanical engineer in the East SF Bay Area with a focus on dynamic languages like Ruby & Python. Goal: learn a ton and become a physics, math, and programming teacher. Message me on DEV.TO