diff options
Diffstat (limited to '11/src/part-1.rs')
-rw-r--r-- | 11/src/part-1.rs | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/11/src/part-1.rs b/11/src/part-1.rs new file mode 100644 index 0000000..163787a --- /dev/null +++ b/11/src/part-1.rs @@ -0,0 +1,76 @@ +mod tensor; + +use std::io::{BufRead}; + +pub type Matrix<T> = tensor::Tensor<T, 2>; +pub type Index = tensor::Index<2>; + + +pub fn main() { + let args: Vec<String> = std::env::args().collect(); + if args.len() != 2 { panic!("Need exactly 1 argument"); } + let num_steps: usize = args[1].parse().expect("Need number of steps as argument"); + + let stdin = std::io::stdin(); + let handle = stdin.lock(); + + let mut energy: Matrix<usize> = { + let mut tmp: Vec<usize> = Vec::new(); + let mut m: usize = 0; + for l in handle.lines() { + let line = l.unwrap(); + tmp.extend(line.chars().map(|c| c.to_digit(10).unwrap() as usize)); + m += 1; + } + let n = tmp.len()/m; + Matrix::new_from([m, n], tmp) + }; + let m = energy.shape()[0]; + let n = energy.shape()[1]; + let mut num_flashes: usize = 0; + + let mut can_flash: Matrix<bool> = Matrix::new(energy.shape().clone(), true); + let mut to_increase: Vec<Index> = Vec::new(); + for s in 0..num_steps { + //println!("Step {}.", s); + + can_flash.fill(true); + to_increase.clear(); + for i in 0..m { + for j in 0..n { + to_increase.push([i, j]); + } + } + + while let Some(idx) = to_increase.pop() { + if can_flash[idx] { + energy[idx] += 1; + if energy[idx] > 9 { + let neighbors: [Index; 8] = [[idx[0] , idx[1].wrapping_sub(1)], [idx[0] , idx[1]+1], + [idx[0].wrapping_sub(1) , idx[1] ], [idx[0]+1, idx[1] ], + [idx[0].wrapping_sub(1) , idx[1].wrapping_sub(1)], [idx[0]+1, idx[1]+1], + [idx[0].wrapping_sub(1) , idx[1]+1 ], [idx[0]+1, idx[1].wrapping_sub(1)] ]; + energy[idx] = 0; + can_flash[idx] = false; + num_flashes += 1; + for neighbor in (& neighbors).into_iter().filter(|neighbor| energy.in_bounds(neighbor)) { + to_increase.push(*neighbor); + } + } + } + } + + /* + println!("Total flashes: {}", num_flashes); + for i in 0..m { + for j in 0..n { + print!("{}", energy[[i, j]]); + } + println!(""); + } + println!("...."); + */ + } + + println!("Number of flashes: {}", num_flashes); +} |