aboutsummaryrefslogtreecommitdiff
path: root/2022/rust/day08/src/main.rs
blob: 4c53e1461ec942f377b7d70ec7ca98ba6d698edb (plain) (blame)
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
91
92
93
94
95
96
use std::env;
use std::fs;

#[derive(Debug)]
struct Tree {
    height: i32,
    visible: bool,
    scenic: i32
}

#[allow(unused_parens)]
fn main() {
    let args = env::args().nth(1).expect("missing input");
    let input = fs::read_to_string(args).unwrap();
    let mut forest: Vec<Vec<Tree>> = input.trim().split("\n")
        .map(|x| x.chars()
            .map(|x| Tree {
                height: x.to_digit(10).unwrap() as i32,
                visible: false,
                scenic: 1
            }).collect()).collect();

    let mut sum = 0;
    let mut max = 0;
    let len = forest.len();

    // not thrilled with this code.
    for i in 0 .. forest.len() {
        let mut min = (-1, -1, -1, -1);
        for j in 0 .. forest.len() {
            let k = forest[0].len() - 1 - j;

            if forest[i][j].height > min.0 {
                min.0 = forest[i][j].height;
                if !forest[i][j].visible {
                    sum += 1;
                }
                forest[i][j].visible = true;
            }
            if forest[i][k].height > min.1 {
                min.1 = forest[i][k].height;
                if !forest[i][k].visible {
                    sum += 1;
                }
                forest[i][k].visible = true;
            }
            if forest[j][i].height > min.2 {
                min.2 = forest[j][i].height;
                if !forest[j][i].visible {
                    sum += 1;
                }
                forest[j][i].visible = true;
            }
            if forest[k][i].height > min.3 {
                min.3 = forest[k][i].height;
                if !forest[k][i].visible {
                    sum += 1;
                }
                forest[k][i].visible = true;
            }

            let min = forest[i][j].height;
            forest[i][j].scenic *= gaze(&forest, (i..=i), (j+1..len), min);
            forest[i][j].scenic *= gaze(&forest, (i+1..len), (j..=j), min);
            forest[i][j].scenic *= gaze(&forest, (i..=i), (0..j).rev(), min);
            forest[i][j].scenic *= gaze(&forest, (0..i).rev(), (j..=j), min);

            if forest[i][j].scenic > max {
                max = forest[i][j].scenic;
            }

        }
    }
    println!("{}", sum);
    println!("{}", max);
}

// bluh why are range and rangeinclusive separate types
// bluhhh why are types not directly generic-able
// bluhhhhhh https://kaylynn.gay/blog/post/rust_ranges_and_suffering

// all things considered i'm happy with this function
// considering how many Interesting behaviors of rust i ran into on the way
fn gaze(forest: &Vec<Vec<Tree>>, x: impl Iterator<Item=usize>,
        y: impl Iterator<Item=usize> + Clone, min: i32) -> i32 {
    let mut count = 0;
    for i in x {
        for j in y.clone() {
            count += 1;
            if forest[i][j].height >= min {
                return count;
            }
        }
    }
    return count;
}