LOADING: An error occurred. Update Chrome, try Firefox, or visit this post for more details.

⚠️Reddit changed how removals work, which breaks Reveddit's website. Install the extension to track removed content:Add to chromeAdd to firefoxWhat changed?
✖︎
about reveddit
⚙F.A.Q.add-ons
r/
status
copy sharelink
[+] show filters
50
SOLUTION MEGATHREAD-🎄- 2021 Day 11 Solutions -🎄-(self.adventofcode)
submitted 4 years, 1 month ago* (edited 1 hour, 2 minutes after) by daggerdragon to /r/adventofcode (134.8k)
826 commentsredditother-discussionssubreddit-indexmessage modsop-focus

NEW AND NOTEWORTHY

[Update @ 00:57]: Visualizations

  • Today's puzzle is going to generate...

... view full text

since 4 years, 1 month ago
1 of 1

Tip Reveddit Real-Time can notify you when your content is removed.

your account history
(check your username's removed content. why?)
Tip Check if your account has any removed comments.
view my removed comments
you are viewing a single comment's thread.
view all comments
[–][deleted]2 points4 years, 1 month ago

Rust

Using a HashMap for the octopuses instead of a 2D array worked pretty well together with helper functions from my Coordinates class.

Stepping logic for both parts

fn step(&mut self) -> usize {
    // First, the energy level of each octopus increases by 1.
    for c in Coordinates::in_area(0..self.width, 0..self.height) {
        self.octopuses.entry(c).and_modify(|e| *e += 1);
    }
    let mut flashed = HashSet::new();
    loop {
        // Then, any octopus with an energy level greater than 9 flashes.
        let flashes = Coordinates::in_area(0..self.width, 0..self.height)
            .filter(|c| *self.octopuses.get(c).unwrap() > 9)
            .filter(|c| !flashed.contains(c))
            .collect::<HashSet<Coordinates>>();

        // This increases the energy level of all adjacent octopuses by 1,
        // including octopuses that are diagonally adjacent.
        for c in flashes.iter().flat_map(|c| c.all_offset_by(1)) {
            self.octopuses.entry(c).and_modify(|e| *e += 1);
        }

        // If this causes an octopus to have an energy level greater than 9, it also flashes.
        // This process continues as long as new octopuses keep having their energy level
        // increased beyond 9.
        if flashes.is_empty() {
            break;
        }
        flashed.extend(flashes);
    }

    // Finally, any octopus that flashed during this step has its energy level set to 0,
    // as it used all of its energy to flash.
    for c in flashed.iter() {
        self.octopuses.entry(*c).and_modify(|e| *e = 0);
    }

    // How many total flashes are there after this step?
    flashed.len()
}
permalinkas-of
r/revedditremoved.substack.com
🚨 NEWS 🚨
✖︎

Important: Reddit Changed How Removals Work

A recent Reddit update makes mod-removed content disappear from profile pages, which breaks Reveddit's website.

Install the browser extension to receive removal alerts.

Add to chromeAdd to firefox

What changed?

r/revedditremoved.substack.com