AoC Day 23: Experimental Emergency Teleportation

twitter logo github logo ・1 min read

Advent of Code (26 Part Series)

1) AoC Day 1: Chronal Calibration 2) AoC Day 2: Inventory Management System 3 ... 24 3) AoC Day 3: No Matter How You Slice It 4) AoC Day 4: Repose Record 5) AoC Day 5: Alchemical Reduction 6) AoC Day 6: Chronal Coordinates 7) AoC Day 7: The Sum of Its Parts 8) AoC Day 8: Memory Maneuver (Placeholder) 9) AoC Day 9: Marble Mania 10) AoC Day 10: The Stars Align 11) AoC Day 11: Chronal Charge 12) AoC Day 12: Subterranean Sustainability 13) AoC Day 13: Mine Cart Madness 14) AoC Day 14: Chocolate Charts 15) AoC Day 15: Beverage Bandits 16) AoC Day 16: Chronal Classification 17) AoC Day 17: Reservoir Research 18) AoC Day 18: Settlers of The North Pole 19) AoC Day 19: Go With the Flow 20) AoC Day 20: A Regular Map 21) AoC Day 21: Chronal Conversion 22) AoC Day 22: Mode Maze 23) AoC Day 23: Experimental Emergency Teleportation 24) AoC Day 24: Immune System Simulator 20XX 25) AoC Day 25: Four-Dimensional Adventure 26) Advent of Code Wrap-Up

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 Jun 13

Async, Parallel, Concurrent Explained - Starring Gordon Ramsay

Complex computing concepts simplified

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