summaryrefslogtreecommitdiff
path: root/2023/day5/src/lib.rs
blob: fb3ef96899c38fc35eaa8ede964701019aeb9696 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use std::str::Split;

fn get_data(data: &mut Split<&str>) -> Vec<Vec<u64>> {
    data.nth(0)
        .unwrap()
        .split(':')
        .nth(1)
        .unwrap()
        .trim_start()
        .split('\n')
        .map(|x| x.split(' ').map(|y| y.parse::<u64>().unwrap()).collect())
        .collect()
}

pub fn part1(input: &str) -> u64 {
    let mut input = input.trim().split("\n\n");

    let mut seeds: Vec<u64> = input
        .nth(0)
        .unwrap()
        .split(':')
        .nth(1)
        .unwrap()
        .trim()
        .split(' ')
        .map(|x| x.parse::<u64>().unwrap())
        .collect();

    let mut data: Vec<Vec<Vec<u64>>> = vec![];
    for _ in 0..7 {
        let x: Vec<Vec<u64>> = get_data(&mut input);

        data.push(x);
    }
    // 0 -> seed-to-soil
    // 1 -> soil-to-fertilizer
    // 2 -> fertilizer-to-water
    // 3 -> water-to-light
    // 4 -> light-to-temperature
    // 5 -> temperature-to-humidity
    // 6 -> humidity-to-location

    for d in &data {
        let mut checked: Vec<u64> = vec![];
        for i in d {
            let drs = i[0];
            let srs = i[1];
            let incr = i[2];

            for seed in &mut seeds {
                if !checked.contains(&*seed) && *seed >= srs && *seed <= srs + incr {
                    *seed = *seed - srs + drs;
                    checked.push(*seed);
                }
            }
        }
    }

    *seeds.iter().min().unwrap()
}

pub fn part2(input: &str) -> u64 {
    let mut input = input.trim().split("\n\n");

    let s: Vec<u64> = input
        .nth(0)
        .unwrap()
        .split(':')
        .nth(1)
        .unwrap()
        .trim()
        .split(' ')
        .map(|x| x.parse::<u64>().unwrap())
        .collect();

    assert_eq!(s.len() % 2, 0);
    let mut seeds: Vec<u64> = vec![];
    let mut i = 0;
    while i < s.len() {
        for j in s[i] as usize..(s[i] + s[i + 1]) as usize {
            seeds.push(j as u64);
        }

        i += 2;
    }
    println!("{seeds:?}");

    let mut data: Vec<Vec<Vec<u64>>> = vec![];
    for _ in 0..7 {
        let x: Vec<Vec<u64>> = get_data(&mut input);

        data.push(x);
    }
    // 0 -> seed-to-soil
    // 1 -> soil-to-fertilizer
    // 2 -> fertilizer-to-water
    // 3 -> water-to-light
    // 4 -> light-to-temperature
    // 5 -> temperature-to-humidity
    // 6 -> humidity-to-location

    for d in &data {
        let mut checked: Vec<u64> = vec![];
        for i in d {
            let drs = i[0];
            let srs = i[1];
            let incr = i[2];

            for seed in &mut seeds {
                if !checked.contains(&*seed) && *seed >= srs && *seed <= srs + incr {
                    *seed = *seed - srs + drs;
                    checked.push(*seed);
                }
            }
        }
    }

    let mut res = seeds[0];
    for seed in seeds {
        if seed < res {
            res = seed;
        }
    }

    res
}

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

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

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

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