r/adventofcode • • Dec 23 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 23 Solutions -🎄-

--- Day 23: Experimental Emergency Teleportation ---


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.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 23

Transcript:

It's dangerous to go alone! Take this: ___


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 at 01:40:41!

22 Upvotes

205 comments sorted by

View all comments

Show parent comment

1

u/m1el Dec 23 '18

Do you have a proof or something?

I don't have a full proof, but here's my line of thought:

The shape of Manhattan range can be defined by the following formulas: x+y+z in range_0 && x+y-z in range_1 && x-y+z in range_2 && x-y-z in range_3.

Intersection of Manhattan distances can be performed by intersecting 1D ranges in those definitions. Due to constraints of initial ranges, that formula will always yield correct results.

fn intersect_1d(a: (isize, isize), b: (isize, isize)) -> Option<(isize, isize)> {
    let rv = (a.0.max(b.0), a.1.min(b.1));
    if rv.0 > rv.1 {
        None
    } else {
        Some(rv)
    }
}

fn intersect(a: &Intersection, b: &Intersection) -> Option<Intersection> {
    Some(Intersection {
        xpypz: intersect_1d(a.xpypz, b.xpypz)?,
        xpymz: intersect_1d(a.xpymz, b.xpymz)?,
        xmypz: intersect_1d(a.xmypz, b.xmypz)?,
        xmymz: intersect_1d(a.xmymz, b.xmymz)?,
    })
}