blob: 63d96afb9fca1ffd8611dd79751f35e6646ce1e4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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);
}
|