Skip to content

Commit

Permalink
complete day 11 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Lawrence Niu committed Dec 5, 2022
1 parent 525e7d8 commit b8dd898
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fs::File;
use std::io::{BufRead, BufReader};
use std::str;

#[allow(dead_code)]
fn debug_octopi(octopi: &[u32]) {
for row in 0..10 {
let start = row * 10;
Expand All @@ -27,9 +28,8 @@ fn empower_adjacent(octopi: &mut [u32], index: usize) {
}
}

fn cascade_flashes(octopi: &mut [u32]) -> u64 {
fn cascade_flashes(octopi: &mut [u32]) {
let mut flashed: Vec<usize> = Vec::new();
let mut num_flashes: u64 = 0;

loop {
let new_flashes: Vec<usize> = octopi
Expand All @@ -44,16 +44,13 @@ fn cascade_flashes(octopi: &mut [u32]) -> u64 {
if new_flashes.is_empty() {
break;
}
num_flashes += new_flashes.len() as u64;
flashed.extend_from_slice(&new_flashes[..]);
flashed.sort();

for flash_index in new_flashes.into_iter() {
empower_adjacent(octopi, flash_index);
}
}

num_flashes
}

fn process_lines(reader: impl BufRead) -> anyhow::Result<u64> {
Expand All @@ -68,13 +65,18 @@ fn process_lines(reader: impl BufRead) -> anyhow::Result<u64> {
octopi.append(&mut powers);
}

let mut num_flashes: u64 = 0;
for _step in 0..100 {
let mut step_count = 0u64;
loop {
octopi.iter_mut().for_each(|octopus| {
*octopus += 1;
});

num_flashes += cascade_flashes(&mut octopi[..]);
cascade_flashes(&mut octopi[..]);
step_count += 1;

if octopi.iter().all(|octopus| 10 <= *octopus) {
break;
}

octopi
.iter_mut()
Expand All @@ -83,8 +85,7 @@ fn process_lines(reader: impl BufRead) -> anyhow::Result<u64> {
*flashed = 0;
});
}

Ok(num_flashes)
Ok(step_count)
}
fn main() {
const INPUT_PATH: &str = "data/input.txt";
Expand All @@ -94,8 +95,8 @@ fn main() {
Err(err) => {
eprintln!("Could not process file {}:\n {}", INPUT_PATH, err);
}
Ok(error_score) => {
println!("error score total: {}", error_score);
Ok(step_count) => {
println!("# steps: {}", step_count);
}
},
Err(err) => {
Expand Down

0 comments on commit b8dd898

Please sign in to comment.