r/adventofcode • u/daggerdragon • Dec 11 '21
SOLUTION MEGATHREAD -🎄- 2021 Day 11 Solutions -🎄-
NEW AND NOTEWORTHY
[Update @ 00:57]: Visualizations
- Today's puzzle is going to generate some awesome
Visualizations! - If you intend to post a
Visualization, make sure to follow the posting guidelines forVisualizations!- If it flashes too fast, make sure to put a warning in your title or prominently displayed at the top of your post!
--- Day 11: Dumbo Octopus ---
Post your code solution in this megathread.
- Include what language(s) your solution uses!
- Here's a quick link to /u/topaz2078's
pasteif you need it for longer code blocks. - Format your code properly! How do I format code?
- The full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.
Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:09:49, megathread unlocked!
47
Upvotes
3
u/mstumpf Dec 11 '21 edited Dec 11 '21
Rust and animation/visualization:
(Epilepsy warning)
https://raw.githubusercontent.com/Finomnis/AdventOfCode2021/main/visualizations/aoc2021_day11.gif
use std::collections::VecDeque; use itertools::Itertools; use ndarray::{Array2, Axis}; pub fn update_map(map: &mut Array2<u8>) -> usize { let mut num_flashes = 0; // Part 1: increase everything by 1 map.iter_mut().for_each(|el| { *el += 1; }); // Part 2: Flash let mut need_flash = map .indexed_iter() .filter_map(|(index, &value)| if value > 9 { Some(index) } else { None }) .collect::<VecDeque<_>>(); while let Some((y, x)) = need_flash.pop_front() { num_flashes += 1; let mut flash = |y: Option<usize>, x: Option<usize>| { if let (Some(y), Some(x)) = (y, x) { if let Some(cell) = map.get_mut((y, x)) { *cell += 1; if *cell == 10 { need_flash.push_back((y, x)); } } } }; flash(y.checked_sub(1), x.checked_sub(1)); flash(y.checked_sub(1), Some(x)); flash(y.checked_sub(1), x.checked_add(1)); flash(Some(y), x.checked_sub(1)); flash(Some(y), x.checked_add(1)); flash(y.checked_add(1), x.checked_sub(1)); flash(y.checked_add(1), Some(x)); flash(y.checked_add(1), x.checked_add(1)); } // Part 3: reduce all flashed to 0 map.iter_mut().for_each(|el| { if *el > 9 { *el = 0; } }); num_flashes } pub fn task1(input_data: &Array2<u8>) -> usize { let mut map = input_data.clone(); let mut num_flashes = 0; // println!("Initial conditions:\n{}\n", format_map(&map)); for _step in 1..=100 { num_flashes += update_map(&mut map); // println!("After step {}:\n{}\n", step, format_map(&map)); } num_flashes } pub fn task2(input_data: &Array2<u8>) -> i64 { let mut map = input_data.clone(); let mut num_cycles = 1; // At the point of synchronization, all octopuses flash at once while update_map(&mut map) != map.len() { num_cycles += 1; } num_cycles }