summaryrefslogtreecommitdiff
path: root/08/src/part-2.rs
diff options
context:
space:
mode:
Diffstat (limited to '08/src/part-2.rs')
-rw-r--r--08/src/part-2.rs90
1 files changed, 90 insertions, 0 deletions
diff --git a/08/src/part-2.rs b/08/src/part-2.rs
new file mode 100644
index 0000000..94bf4d5
--- /dev/null
+++ b/08/src/part-2.rs
@@ -0,0 +1,90 @@
+mod tensor;
+
+use std::cmp::{max};
+use std::io::{BufRead};
+
+use tensor::{Index, Shape, Tensor};
+
+fn main() {
+ let stdin = std::io::stdin();
+ let mut handle = stdin.lock();
+
+ let mut tmp: Vec<usize> = Vec::new();
+ let mut m: usize = 0;
+ let mut n: usize = 0;
+ let mut n_check: usize = 0;
+
+ loop {
+ let buf = handle.fill_buf().expect("IO error");
+ let bytes_read = buf.len();
+ if bytes_read == 0 { break; }
+
+ for & b in buf.into_iter() {
+ if b >= b'0' && b <= b'9' {
+ tmp.push((b - b'0').try_into().unwrap());
+ if m == 0 {
+ n += 1;
+ }
+ n_check += 1;
+ }
+ if b == b'\n' {
+ m += 1;
+ assert_eq!(n_check, n);
+ n_check = 0;
+ }
+ }
+
+ handle.consume(bytes_read);
+ }
+
+ let mut x: Tensor<usize, 2> = Tensor::new_from([m, n], tmp);
+ let shape = x.shape().clone();
+
+ let mut highest_score: usize = 0;
+
+ for i in 0..m {
+ for j in 0..n {
+ let h = x[[i, j]];
+
+ // Look north
+ let mut north_dist = 0;
+ for a in (0..i).rev() {
+ north_dist += 1;
+ if x[[a, j]] >= h {
+ break;
+ }
+ }
+
+ // Look south.
+ let mut south_dist = 0;
+ for a in i+1..m {
+ south_dist += 1;
+ if x[[a, j]] >= h {
+ break;
+ }
+ }
+
+ // Look east.
+ let mut east_dist = 0;
+ for a in j+1..n {
+ east_dist += 1;
+ if x[[i, a]] >= h {
+ break;
+ }
+ }
+
+ // Look west.
+ let mut west_dist = 0;
+ for a in (0..j).rev() {
+ west_dist += 1;
+ if x[[i, a]] >= h {
+ break;
+ }
+ }
+
+ highest_score = max(highest_score, north_dist*south_dist*east_dist*west_dist);
+ }
+ }
+
+ println!("{}", highest_score);
+}