diff --git a/december_11/Cargo.toml b/december_11/Cargo.toml new file mode 100644 index 0000000..bcef53a --- /dev/null +++ b/december_11/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "december_11" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/december_11/example.txt b/december_11/example.txt new file mode 100644 index 0000000..a3819c9 --- /dev/null +++ b/december_11/example.txt @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526 \ No newline at end of file diff --git a/december_11/input.txt b/december_11/input.txt new file mode 100644 index 0000000..d468d51 --- /dev/null +++ b/december_11/input.txt @@ -0,0 +1,10 @@ +6318185732 +1122687135 +5173237676 +8754362612 +5718474666 +8443654137 +1247634346 +1446514585 +6717288267 +1727871228 \ No newline at end of file diff --git a/december_11/src/main.rs b/december_11/src/main.rs new file mode 100644 index 0000000..3228cdc --- /dev/null +++ b/december_11/src/main.rs @@ -0,0 +1,92 @@ +use std::fs::File; +use std::io::{BufReader}; +use std::io::prelude::*; + +const RADIX: u32 = 10; + +static mut LINE_VEC: Vec> = Vec::new(); +static mut TOTAL_FLASHES: u32 = 0; + +const STEPS: u32 = 300; + +fn main(){ + let file = File::open("./input.txt").expect("Read failed"); + let reader = BufReader::new(file); + + + + for line in reader.lines(){ + let line_as_string = line.unwrap(); + let line_as_int_vec = line_as_string.chars().map(|x| x.to_digit(RADIX).unwrap()).collect::>(); + unsafe{LINE_VEC.push(line_as_int_vec);} + + } + unsafe { + let height = LINE_VEC.len(); + let width = LINE_VEC[0].len(); + + + for _i in 1..=STEPS { + // Energy of each Octopus increases by 1 + for line_nr in 0..height { + for column_nr in 0..width { + LINE_VEC[line_nr][column_nr] += 1; + } + } + + // Flash all Octopus over 9 and reset to 9 + for line_nr in 0..height { + for column_nr in 0..width { + if LINE_VEC[line_nr][column_nr] > 9 { + flash(line_nr, column_nr, height, width); + } + } + } + // Reset all flashed to 0 + for line_nr in 0..height { + for column_nr in 0..width { + if LINE_VEC[line_nr][column_nr] >98 { + LINE_VEC[line_nr][column_nr] = 0; + } + } + } + // Check if all flash + let mut all_flashed = true; + for line_nr in 0..height { + for column_nr in 0..width { + if LINE_VEC[line_nr][column_nr] != 0 { + all_flashed = false; + } + } + } + if all_flashed {println!("All Flashed at step {}", _i);} + } + + } + + unsafe { println!("Total flashes: {}", TOTAL_FLASHES); } +} + + +fn flash(line_nr: usize, column_nr: usize, height: usize, width: usize) { + unsafe { + //Set Current Octopus to 0 + if LINE_VEC[line_nr][column_nr] == 10 || LINE_VEC[line_nr][column_nr] == 9 { + LINE_VEC[line_nr][column_nr] = 99; + TOTAL_FLASHES += 1; + + if line_nr > 0 && column_nr > 0 {flash(line_nr - 1, column_nr - 1, height, width);} + if line_nr > 0 {flash(line_nr - 1, column_nr, height, width);} + if line_nr > 0 && column_nr < width - 1 {flash(line_nr - 1, column_nr + 1, height, width);} + + if column_nr > 0 {flash(line_nr, column_nr - 1, height, width);} + if column_nr < width - 1 {flash(line_nr , column_nr + 1, height, width);} + + if line_nr < height - 1 && column_nr > 0 {flash(line_nr + 1, column_nr - 1, height, width);} + if line_nr < height - 1 {flash(line_nr + 1, column_nr, height, width);} + if line_nr < height - 1 && column_nr < width - 1 {flash(line_nr + 1, column_nr + 1, height, width);} + } else { + LINE_VEC[line_nr][column_nr] += 1; + } + } +}