summaryrefslogtreecommitdiff
path: root/2023/rust/day9/src/lib.rs
blob: a1b0588a5238e20d298e2f9a1d06c3cbcf6024ad (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
pub fn part1(input: &str) -> i32 {
    let mut res: i32 = 0;

    let m: Vec<Vec<i32>> = input
        .trim_end()
        .split('\n')
        .map(|x| {
            x.split(' ')
                .map(|y| y.parse::<i32>().unwrap())
                .collect::<Vec<i32>>()
        })
        .collect();

    for i in m {
        let mut aux: Vec<Vec<i32>> = vec![i];

        let mut z = 1;
        loop {
            aux.push(vec![]);
            for j in 1..aux[z - 1].len() {
                let x = aux[z - 1][j] - aux[z - 1][j - 1];
                aux[z].push(x);
            }

            if aux[z].iter().filter(|&k| *k != 0).count() == 0 {
                aux[z].push(0);
                break;
            }

            z += 1;
        }

        let n = aux.len() - 1;
        for j in (0..n).rev() {
            let t = aux[j + 1][aux[j + 1].len() - 1];
            let r = aux[j][aux[j].len() - 1];
            aux[j].push(t + r);
        }

        res += aux[0][aux[0].len() - 1];
    }

    res
}

pub fn part2(input: &str) -> i32 {
    let mut res: i32 = 0;

    let m: Vec<Vec<i32>> = input
        .trim_end()
        .split('\n')
        .map(|x| {
            x.split(' ')
                .map(|y| y.parse::<i32>().unwrap())
                .collect::<Vec<i32>>()
        })
        .collect();

    for i in m {
        let mut aux: Vec<Vec<i32>> = vec![i];

        let mut z = 1;
        loop {
            aux.push(vec![]);
            for j in 1..aux[z - 1].len() {
                let x = aux[z - 1][j] - aux[z - 1][j - 1];
                aux[z].push(x);
            }

            if aux[z].iter().filter(|&k| *k != 0).count() == 0 {
                aux[z].push(0);
                break;
            }

            z += 1;
        }

        let n = aux.len() - 1;
        for j in (0..n).rev() {
            let t = aux[j + 1][0];
            let r = aux[j][0];
            aux[j].insert(0, r - t);
        }

        res += aux[0][0];
    }

    res
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn example_part1() {
        let input = include_str!("../example.txt");
        let result = part1(input);
        assert_eq!(result, 114);
    }

    #[test]
    fn input_part1() {
        let input = include_str!("../input.txt");
        let result = part1(input);
        assert_eq!(result, 2008960228);
    }

    #[test]
    fn example_part2() {
        let input = include_str!("../example.txt");
        let result = part2(input);
        assert_eq!(result, 2);
    }

    #[test]
    fn input_part2() {
        let input = include_str!("../input.txt");
        let result = part2(input);
        assert_eq!(result, 71585);
    }
}