summaryrefslogtreecommitdiff
path: root/08/src/part-2.rs
blob: 94bf4d5991861082401b0d0576b3ee1e294fb572 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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);
}