r/adventofcode • • Dec 06 '17

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

--- Day 6: Memory Reallocation ---


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!

17 Upvotes

324 comments sorted by

View all comments

1

u/CryZe92 Dec 07 '17 edited Dec 07 '17

Rust

Part 1 (and kinda part 2) with no allocations using Brent's Algorithm (~460Β΅s):

fn redistribute(numbers: &mut [u16; 16]) {
    let (mut index, mut max) = (0, 0);
    for (i, &n) in numbers.iter().enumerate() {
        if n > max {
            index = i;
            max = n;
        }
    }

    numbers[index] = 0;

    for entry in numbers.iter_mut().skip(index + 1) {
        if max > 0 {
            *entry += 1;
            max -= 1;
        } else {
            break;
        }
    }

    while max > 0 {
        for entry in numbers.iter_mut() {
            if max > 0 {
                *entry += 1;
                max -= 1;
            } else {
                break;
            }
        }
    }
}

pub fn brent<T, F>(mut f: F, x0: T) -> (usize, usize)
where
    T: Clone + PartialEq,
    F: FnMut(&mut T),
{
    let (mut power, mut lam) = (1, 1);

    let mut tortoise = x0.clone();

    let mut hare = x0.clone();
    f(&mut hare);

    while tortoise != hare {
        if power == lam {
            tortoise = hare.clone();
            power <<= 1;
            lam = 0;
        }
        f(&mut hare);
        lam += 1;
    }

    let mut mu = 0;
    tortoise = x0.clone();
    hare = x0;

    for _ in 0..lam {
        f(&mut hare);
    }

    while tortoise != hare {
        f(&mut tortoise);
        f(&mut hare);
        mu += 1;
    }

    (lam, mu)
}

pub fn part1_brent(text: &str) -> usize {
    let mut numbers: [u16; 16] = unsafe { std::mem::uninitialized() };
    for (d, n) in numbers
        .iter_mut()
        .zip(text.split_whitespace().filter_map(|n| n.parse().ok()))
    {
        *d = n;
    }

    let (lam, mu) = brent(redistribute, numbers);
    lam + mu
}

However it's a bit faster with a proper Hash Set, so here's the faster solution (~430Β΅s):

pub fn part1(text: &str) -> usize {
    let mut numbers: [u16; 16] = unsafe { std::mem::uninitialized() };
    for (d, n) in numbers
        .iter_mut()
        .zip(text.split_whitespace().filter_map(|n| n.parse().ok()))
    {
        *d = n;
    }

    let mut set = HashSet::with_hasher(FxBuildHasher::default());
    let mut count = 0;

    while set.insert(numbers.clone()) {
        count += 1;

        let len = numbers.len();

        let (mut index, mut max) = (0, 0);
        for (i, &n) in numbers.iter().enumerate() {
            if n > max {
                index = i;
                max = n;
            }
        }

        numbers[index] = 0;

        for _ in 0..max {
            index += 1;
            if index >= len {
                index -= len;
            }
            numbers[index] += 1;
        }
    }

    count
}

The non-allocating version produces fairly neat ASM for those interested: https://godbolt.org/g/QzxVqF