AoC Day 23: Experimental Emergency Teleportation

・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!

DISCUSSION (2)
 

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 Nov 19 '18

The node_modules problem

Why node_modules is so heavy and can be done to solve it?

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