diff options
author | Santo Cariotti <santo@dcariotti.me> | 2023-12-05 19:12:55 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2023-12-05 19:12:55 +0100 |
commit | 4301b064d443b13854dd42ef8242d20bef548fe9 (patch) | |
tree | c7708803e3c9fc192f445b1b4012e9dcba385a87 /2023/day5/src | |
parent | 5580c538d09db86706e717b9c8e4f74d27449855 (diff) |
part 1 of day5
Diffstat (limited to '2023/day5/src')
-rw-r--r-- | 2023/day5/src/lib.rs | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/2023/day5/src/lib.rs b/2023/day5/src/lib.rs new file mode 100644 index 0000000..aab4feb --- /dev/null +++ b/2023/day5/src/lib.rs @@ -0,0 +1,166 @@ +use std::str::Split; + +fn get_data(data: &mut Split<&str>) -> Vec<Vec<i64>> { + data.nth(0) + .unwrap() + .split(':') + .nth(1) + .unwrap() + .trim_start() + .split('\n') + .map(|x| x.split(' ').map(|y| y.parse::<i64>().unwrap()).collect()) + .collect() +} + +pub fn part1(input: &str) -> i64 { + let mut input = input.trim().split("\n\n"); + + let mut seeds: Vec<i64> = input + .nth(0) + .unwrap() + .split(':') + .nth(1) + .unwrap() + .trim() + .split(' ') + .map(|x| x.parse::<i64>().unwrap()) + .collect(); + + let mut data: Vec<Vec<Vec<i64>>> = vec![]; + for _ in 0..7 { + let x: Vec<Vec<i64>> = 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<i64> = 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 +} + +pub fn part2(input: &str) -> i64 { + let mut input = input.trim().split("\n\n"); + + let s: Vec<i64> = input + .nth(0) + .unwrap() + .split(':') + .nth(1) + .unwrap() + .trim() + .split(' ') + .map(|x| x.parse::<i64>().unwrap()) + .collect(); + + assert_eq!(s.len() % 2, 0); + let mut seeds: Vec<i64> = 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 i64); + } + + i += 2; + } + println!("{seeds:?}"); + + let mut data: Vec<Vec<Vec<i64>>> = vec![]; + for _ in 0..7 { + let x: Vec<Vec<i64>> = 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<i64> = 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); + } +} |