From 94d8d1dbbe5be6a53ffae0b7fe6ebc83c1753ff3 Mon Sep 17 00:00:00 2001 From: mflinn-broad Date: Sat, 11 Dec 2021 11:04:28 -0500 Subject: [PATCH] finish day 11 --- src/days/day11.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 4 deletions(-) diff --git a/src/days/day11.rs b/src/days/day11.rs index 2381688..7768978 100644 --- a/src/days/day11.rs +++ b/src/days/day11.rs @@ -3,10 +3,11 @@ use crate::util; pub fn run() { let raw_input = util::read_input("inputs/day11.txt").unwrap(); let input = process(&raw_input); - println!("Part 1: {}", part_1(input)); + println!("Part 1: {}", part_1(input.clone())); + println!("Part 2: {}", part_2(input)); } -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] struct Octopus { e_level: u32, flashed_on_step: bool, @@ -118,7 +119,7 @@ fn propagate_flash(pos: Position, grid: &mut Vec>) { fn part_1(input: Vec>) -> usize { let mut input = input; let mut flash_counter: usize = 0; - for step in 0..100 { + for _ in 0..100 { for row in 0..input.len() { for col in 0..input[0].len() { input[row][col].increase(); @@ -146,8 +147,41 @@ fn part_1(input: Vec>) -> usize { flash_counter } +fn part_2(input: Vec>) -> usize { + let mut input = input; + for step in 0.. { + let mut flash_counter = 0; + for row in 0..input.len() { + for col in 0..input[0].len() { + input[row][col].increase(); + } + } + for row in 0..input.len() { + for col in 0..input[0].len() { + if input[row][col].e_level > 9 && !input[row][col].flashed_on_step { + let pos = Position { row, col }; + input[row][col].flashed_on_step = true; + propagate_flash(pos, &mut input); + } + } + } + for row in 0..input.len() { + for col in 0..input[0].len() { + if input[row][col].flashed_on_step { + flash_counter += 1; + input[row][col].e_level = 0; + input[row][col].flashed_on_step = false; + } + } + } + if flash_counter == 100 { + return step + 1; + } + } + 0 +} + fn process(input: &str) -> Vec> { - input.lines() .map(|line| { line.chars() @@ -159,3 +193,28 @@ fn process(input: &str) -> Vec> { }) .collect() } + +#[cfg(test)] +mod tests { + use super::*; + extern crate test; + use test::Bencher; + + #[bench] + fn bench_part_1(b: &mut Bencher) { + let raw_input = util::read_input("inputs/day11.txt").unwrap(); + b.iter(|| { + let input = process(&raw_input); + part_1(input); + }); + } + + #[bench] + fn bench_part_2(b: &mut Bencher) { + let raw_input = util::read_input("inputs/day11.txt").unwrap(); + b.iter(|| { + let input = process(&raw_input); + part_2(input); + }); + } +}