diff options
Diffstat (limited to '09/src/part-1.rs')
-rw-r--r-- | 09/src/part-1.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/09/src/part-1.rs b/09/src/part-1.rs new file mode 100644 index 0000000..63d96af --- /dev/null +++ b/09/src/part-1.rs @@ -0,0 +1,41 @@ +mod tensor; + +use std::io::{BufRead}; + +pub type Matrix = tensor::Tensor<usize, 2>; +pub type Index = tensor::Index<2>; + +pub fn main() { + let mut stdin = std::io::stdin(); + let mut handle = stdin.lock(); + + let x: Matrix = { + 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 mut neighbors: Vec<Option<& usize>> = Vec::new(); + let mut low_points: Vec<usize> = Vec::new(); + for i in 0..x.shape()[0] { + for j in 0..x.shape()[1] { + neighbors.clear(); + neighbors.push(x.el(& [i, j.wrapping_sub(1)])); + neighbors.push(x.el(& [i, j+1])); + neighbors.push(x.el(& [i.wrapping_sub(1), j])); + neighbors.push(x.el(& [i+1, j])); + + let min_neighbor: usize = (& neighbors).into_iter().filter_map(|& y| y).map(|& y| y).min().unwrap(); + if min_neighbor > x[[i, j]] { low_points.push(x[[i, j]]); } + } + } + + let total_risk: usize = low_points.into_iter().map(|l| l+1).sum(); + println!("Total risk: {}", total_risk); +} |