r/adventofcode • • Dec 03 '17

SOLUTION MEGATHREAD -πŸŽ„- 2017 Day 3 Solutions -πŸŽ„-

--- Day 3: Spiral Memory ---


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.


Need a hint from the Hugely* Handy† Haversack‑ of HelpfulΒ§ HintsΒ€?

Spoiler


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!

22 Upvotes

297 comments sorted by

View all comments

Show parent comment

3

u/boscop Dec 03 '17 edited Dec 03 '17

I just discovered Advent of Code today, so I'm a bit late, but here is my solution in Rust for day3:

#![feature(inclusive_range_syntax)]

use std::io::*;

type I = i32;
type U = usize;

fn main() {
    let stdin = stdin();
    for line in stdin.lock().lines().filter_map(|line| line.ok()).filter(|line| line.trim() != "") {
        let pos = line.parse::<I>().unwrap();
        println!("{}: {}, {}", pos, manhattan(pos), part2(pos));
    }
}

fn layer_index(p: I) -> (I, I, I, I) {
    if p == 1 { return (0, 0, 0, 0); }
    fn last_on_layer(i: I) -> I { let x = 2*i+1; x*x }
    let layer = (0..).find(|&i| p <= last_on_layer(i)).unwrap();
    let start = last_on_layer(layer - 1) + 1;
    let zero = start + layer - 1;
    let pos_rel_to_quadrant_start = mod_pos(p - zero, 2 * layer);
    let dist_from_edge_center = if pos_rel_to_quadrant_start > layer {
        2 * layer - pos_rel_to_quadrant_start
    } else {
        pos_rel_to_quadrant_start
    };
    let layer_len = 2 * layer * 4;
    let quadrant = mod_pos((p - zero), layer_len) / (2 * layer);
    (layer, dist_from_edge_center, quadrant, pos_rel_to_quadrant_start)
}

fn manhattan(p: I) -> I {
    let (layer, dist_from_edge_center, _, _) = layer_index(p);
    layer + dist_from_edge_center
}

pub fn mod_pos(a: I, b: I) -> I { (a % b + b) % b }

fn part2(target: I) -> I {
    let size = 1000;
    let mut a = vec![vec![0; size as U]; size as U];
    let c = size / 2;
    a[c as U][c as U] = 1;
    for p in 2.. {
        fn coords(p: I) -> (I, I) {
            let (layer, dist_from_edge_center, quadrant, pos_rel_to_quadrant_start) = layer_index(p);
            // pos in quadrant 0
            let (x, y) = if pos_rel_to_quadrant_start > layer {
                (dist_from_edge_center, layer)
            } else {
                (layer, dist_from_edge_center)
            };
            // rotate to actual quadrant
            match quadrant {
                0 => (x, y),   // top right
                1 => (-y, x),  // top left
                2 => (-x, -y), // bottom left
                3 => (y, -x),  // bottom right
                _ => unreachable!()
            }
        }
        let (px, py) = coords(p);
        let sum = {
            let a_ = &a;
            (-1..=1).flat_map(|y| (-1..=1).map(move |x|
                a_[(c + py + y) as U][(c + px + x) as U]
            )).sum::<I>()
        };
        let a_ = &mut a[(c + py) as U][(c + px) as U];
        *a_ = sum;
        if *a_ > target {
            return *a_;
        }
    }
    -1
}

And I was also disappointed that I couldn't build on part1 to solve part2 :)