From b93563d249cc8dcac7e645bea67d5645010804de Mon Sep 17 00:00:00 2001 From: Julius de Jeu Date: Sat, 11 Dec 2021 09:45:35 +0100 Subject: [PATCH] Day 11 --- input/day11 | 10 ++++ src/day11.rs | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 5 +- 3 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 input/day11 create mode 100644 src/day11.rs diff --git a/input/day11 b/input/day11 new file mode 100644 index 0000000..44e9f75 --- /dev/null +++ b/input/day11 @@ -0,0 +1,10 @@ +2566885432 +3857414357 +6761543247 +5477332114 +3731585385 +1716783173 +1277321612 +3371176148 +1162578285 +6144726367 \ No newline at end of file diff --git a/src/day11.rs b/src/day11.rs new file mode 100644 index 0000000..9701fc6 --- /dev/null +++ b/src/day11.rs @@ -0,0 +1,145 @@ +use std::collections::HashMap; +use crate::Day; + +type FlashMap = HashMap<(i32, i32), i32>; + +pub struct Day11(FlashMap); + +impl Day for Day11 { + fn init(content: String) -> anyhow::Result { + let mut map = HashMap::new(); + for (y, n) in content.lines().enumerate() { + for (x, char) in n.chars().enumerate() { + map.insert((x as i32, y as i32), char.to_string().parse::()?); + } + } + Ok(Self(map)) + } + + fn part1(&self) -> anyhow::Result { + let mut hm = self.0.clone(); + let mut flashes = 0; + for _ in 0..100 { + hm.iter_mut().for_each(|(_, x)| *x += 1); + let mut flashing = get_flashes(&hm); + let mut flashed = vec![]; + do_flash(&mut hm, &mut flashing, &mut flashed); + + let flashing = get_flashes(&hm); + for f in &flashing { + if let Some(entry) = hm.get_mut(f) { + *entry = 0; + } + } + flashes += flashing.len(); + } + + Ok(format!("{}", flashes)) + } + + fn part2(&self) -> anyhow::Result { + let mut hm = self.0.clone(); + for num in 0..1000 { + hm.iter_mut().for_each(|(_, x)| *x += 1); + let mut flashing = get_flashes(&hm); + let mut flashed = vec![]; + do_flash(&mut hm, &mut flashing, &mut flashed); + if flashed.len() == 100 { + return Ok(format!("{}", num + 1)); + } + let flashing = get_flashes(&hm); + for f in &flashing { + if let Some(entry) = hm.get_mut(f) { + *entry = 0; + } + } + } + Err(anyhow::Error::msg("It did not flash 100 times :(")) + } +} + +fn get_flashes(hm: &FlashMap) -> Vec<(i32, i32)> { + hm.iter().filter(|(_, &x)| x > 9).map(|(&p, _)| p).collect::>() +} + +fn do_flash(mut hm: &mut FlashMap, mut flashing: &mut Vec<(i32, i32)>, mut flashed: &mut Vec<(i32, i32)>) { + if let Some((x, y)) = flashing.pop() { + flashed.push((x, y)); + for xn in (x - 1)..=(x + 1) { + for yn in (y - 1)..=(y + 1) { + if let Some(e) = hm.get_mut(&(xn, yn)) { + *e += 1; + if *e > 9 { + if !flashed.contains(&(xn, yn)) && !flashing.contains(&(xn, yn)) { + flashing.push((xn, yn)) + } + } + } + } + } + do_flash(&mut hm, &mut flashing, &mut flashed) + } +} + +fn print_flashes(map: &FlashMap, x_max: i32, y_max: i32) { + let mut v: Vec> = Vec::new(); + for y in 0..y_max { + let mut col = Vec::new(); + for x in 0..x_max { + col.push(*map.get(&(x, y)).unwrap()); + } + v.push(col); + } + for line in v { + for ch in line { + print!("{}", ch) + } + println!() + } + println!() +} + +#[cfg(test)] +mod tests { + use crate::Day; + use crate::day11::Day11; + + const INPUT: &str = r"5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526"; + + #[test] + fn part1_test() -> anyhow::Result<()> { + let d = Day11::init(INPUT.to_string())?; + assert_eq!("1656", d.part1()?); + Ok(()) + } + + #[test] + fn part2_test() -> anyhow::Result<()> { + let d = Day11::init(INPUT.to_string())?; + assert_eq!("195", d.part2()?); + Ok(()) + } + + #[test] + fn part1_real() -> anyhow::Result<()> { + let d = Day11::init(crate::load_input("11")?)?; + assert_eq!("1647", d.part1()?); + Ok(()) + } + + #[test] + fn part2_real() -> anyhow::Result<()> { + let d = Day11::init(crate::load_input("11")?)?; + assert_eq!("348", d.part2()?); + Ok(()) + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index d60ce3c..fa76862 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ use anyhow::Result; use crate::day08::Day08; use crate::day09::Day09; use crate::day10::Day10; +use crate::day11::Day11; mod day; mod day01; @@ -25,6 +26,7 @@ mod day07; mod day08; mod day09; mod day10; +mod day11; fn load_input(day: &str) -> Result { read_to_string(format!("./input/day{}", day)).map_err(|x| x.into()) @@ -69,7 +71,8 @@ fn main() -> anyhow::Result<()> { Box::new(Day07::init(load_input("07")?)?), Box::new(Day08::init(load_input("08")?)?), Box::new(Day09::init(load_input("09")?)?), - Box::new(Day10::init(load_input("10")?)?),]; + Box::new(Day10::init(load_input("10")?)?), + Box::new(Day11::init(load_input("11")?)?),]; let _verbosity = matches.occurrences_of("v"); if matches.is_present("all") {