diff --git a/src/day5/1.rs b/src/day5/1.rs index 54a663b..77867e0 100644 --- a/src/day5/1.rs +++ b/src/day5/1.rs @@ -16,140 +16,38 @@ fn process(data: &str) -> u64 { .unwrap() .collect(); - let seed_to_soil: Vec<(Range, Range)> = lines - .next() - .map(|x| { - x.lines().skip(1).map(|line| { - let y: Vec = line.split(' ').map(|y| y.parse::().unwrap()).collect(); - let size = y[2]; + let maps: Vec, Range)>> = lines + .map(|lines| { + lines + .lines() + .skip(1) + .map(|line| { + let y: Vec = line + .split_ascii_whitespace() + .map(|y| y.parse::().unwrap()) + .collect(); + let size = y[2]; - (y[1]..y[1] + size, y[0]..y[0] + size) - }) + (y[1]..y[1] + size, y[0]..y[0] + size) + }) + .collect() }) - .unwrap() - .collect(); - - let soil_to_ferti: Vec<(Range, Range)> = lines - .next() - .map(|x| { - x.lines().skip(1).map(|line| { - let y: Vec = line.split(' ').map(|y| y.parse::().unwrap()).collect(); - let size = y[2]; - - (y[1]..y[1] + size, y[0]..y[0] + size) - }) - }) - .unwrap() - .collect(); - - let ferti_to_water: Vec<(Range, Range)> = lines - .next() - .map(|x| { - x.lines().skip(1).map(|line| { - let y: Vec = line.split(' ').map(|y| y.parse::().unwrap()).collect(); - let size = y[2]; - - (y[1]..y[1] + size, y[0]..y[0] + size) - }) - }) - .unwrap() - .collect(); - - let water_to_light: Vec<(Range, Range)> = lines - .next() - .map(|x| { - x.lines().skip(1).map(|line| { - let y: Vec = line.split(' ').map(|y| y.parse::().unwrap()).collect(); - let size = y[2]; - - (y[1]..y[1] + size, y[0]..y[0] + size) - }) - }) - .unwrap() - .collect(); - - let light_to_temp: Vec<(Range, Range)> = lines - .next() - .map(|x| { - x.lines().skip(1).map(|line| { - let y: Vec = line.split(' ').map(|y| y.parse::().unwrap()).collect(); - let size = y[2]; - - (y[1]..y[1] + size, y[0]..y[0] + size) - }) - }) - .unwrap() - .collect(); - - let temp_to_humidity: Vec<(Range, Range)> = lines - .next() - .map(|x| { - x.lines().skip(1).map(|line| { - let y: Vec = line.split(' ').map(|y| y.parse::().unwrap()).collect(); - let size = y[2]; - - (y[1]..y[1] + size, y[0]..y[0] + size) - }) - }) - .unwrap() - .collect(); - let humidity_to_loc: Vec<(Range, Range)> = lines - .next() - .map(|x| { - x.lines().skip(1).map(|line| { - let y: Vec = line.split(' ').map(|y| y.parse::().unwrap()).collect(); - let size = y[2]; - - (y[1]..y[1] + size, y[0]..y[0] + size) - }) - }) - .unwrap() .collect(); let mut answer = std::u64::MAX; - for seed in seeds { - let soil = seed_to_soil - .iter() - .find(|x| x.0.contains(&seed)) - .map(|x| seed + x.1.start - x.0.start) - .unwrap_or(seed); + for mut src in seeds { + for map in maps.iter() { + let dst = map + .iter() + .find(|x| x.0.contains(&src)) + .map(|x| src + x.1.start - x.0.start) + .unwrap_or(src); - let ferti = soil_to_ferti - .iter() - .find(|x| x.0.contains(&soil)) - .map(|x| soil + x.1.start - x.0.start) - .unwrap_or(soil); + src = dst; + } - let water = ferti_to_water - .iter() - .find(|x| x.0.contains(&ferti)) - .map(|x| ferti + x.1.start - x.0.start) - .unwrap_or(ferti); - - let light = water_to_light - .iter() - .find(|x| x.0.contains(&water)) - .map(|x| water + x.1.start - x.0.start) - .unwrap_or(water); - - let temp = light_to_temp - .iter() - .find(|x| x.0.contains(&light)) - .map(|x| light + x.1.start - x.0.start) - .unwrap_or(light); - let humidity = temp_to_humidity - .iter() - .find(|x| x.0.contains(&temp)) - .map(|x| temp + x.1.start - x.0.start) - .unwrap_or(temp); - let loc = humidity_to_loc - .iter() - .find(|x| x.0.contains(&humidity)) - .map(|x| humidity + x.1.start - x.0.start) - .unwrap_or(humidity); - - answer = std::cmp::min(answer, loc); + answer = std::cmp::min(answer, src); } answer diff --git a/src/day5/2.rs b/src/day5/2.rs index 5a809a3..20ede3f 100644 --- a/src/day5/2.rs +++ b/src/day5/2.rs @@ -25,140 +25,38 @@ fn process(data: &str) -> u64 { }) .unwrap(); - let seed_to_soil: Vec<(Range, Range)> = lines - .next() - .map(|x| { - x.lines().skip(1).map(|line| { - let y: Vec = line.split(' ').map(|y| y.parse::().unwrap()).collect(); - let size = y[2]; + let maps: Vec, Range)>> = lines + .map(|lines| { + lines + .lines() + .skip(1) + .map(|line| { + let y: Vec = line + .split_ascii_whitespace() + .map(|y| y.parse::().unwrap()) + .collect(); + let size = y[2]; - (y[1]..y[1] + size, y[0]..y[0] + size) - }) + (y[1]..y[1] + size, y[0]..y[0] + size) + }) + .collect() }) - .unwrap() - .collect(); - - let soil_to_ferti: Vec<(Range, Range)> = lines - .next() - .map(|x| { - x.lines().skip(1).map(|line| { - let y: Vec = line.split(' ').map(|y| y.parse::().unwrap()).collect(); - let size = y[2]; - - (y[1]..y[1] + size, y[0]..y[0] + size) - }) - }) - .unwrap() - .collect(); - - let ferti_to_water: Vec<(Range, Range)> = lines - .next() - .map(|x| { - x.lines().skip(1).map(|line| { - let y: Vec = line.split(' ').map(|y| y.parse::().unwrap()).collect(); - let size = y[2]; - - (y[1]..y[1] + size, y[0]..y[0] + size) - }) - }) - .unwrap() - .collect(); - - let water_to_light: Vec<(Range, Range)> = lines - .next() - .map(|x| { - x.lines().skip(1).map(|line| { - let y: Vec = line.split(' ').map(|y| y.parse::().unwrap()).collect(); - let size = y[2]; - - (y[1]..y[1] + size, y[0]..y[0] + size) - }) - }) - .unwrap() - .collect(); - - let light_to_temp: Vec<(Range, Range)> = lines - .next() - .map(|x| { - x.lines().skip(1).map(|line| { - let y: Vec = line.split(' ').map(|y| y.parse::().unwrap()).collect(); - let size = y[2]; - - (y[1]..y[1] + size, y[0]..y[0] + size) - }) - }) - .unwrap() - .collect(); - - let temp_to_humidity: Vec<(Range, Range)> = lines - .next() - .map(|x| { - x.lines().skip(1).map(|line| { - let y: Vec = line.split(' ').map(|y| y.parse::().unwrap()).collect(); - let size = y[2]; - - (y[1]..y[1] + size, y[0]..y[0] + size) - }) - }) - .unwrap() - .collect(); - let humidity_to_loc: Vec<(Range, Range)> = lines - .next() - .map(|x| { - x.lines().skip(1).map(|line| { - let y: Vec = line.split(' ').map(|y| y.parse::().unwrap()).collect(); - let size = y[2]; - - (y[1]..y[1] + size, y[0]..y[0] + size) - }) - }) - .unwrap() .collect(); let mut answer = std::u64::MAX; - for seed in seeds { - let soil = seed_to_soil - .iter() - .find(|x| x.0.contains(&seed)) - .map(|x| seed + x.1.start - x.0.start) - .unwrap_or(seed); + for mut src in seeds { + for map in maps.iter() { + let dst = map + .iter() + .find(|x| x.0.contains(&src)) + .map(|x| src + x.1.start - x.0.start) + .unwrap_or(src); - let ferti = soil_to_ferti - .iter() - .find(|x| x.0.contains(&soil)) - .map(|x| soil + x.1.start - x.0.start) - .unwrap_or(soil); + src = dst; + } - let water = ferti_to_water - .iter() - .find(|x| x.0.contains(&ferti)) - .map(|x| ferti + x.1.start - x.0.start) - .unwrap_or(ferti); - - let light = water_to_light - .iter() - .find(|x| x.0.contains(&water)) - .map(|x| water + x.1.start - x.0.start) - .unwrap_or(water); - - let temp = light_to_temp - .iter() - .find(|x| x.0.contains(&light)) - .map(|x| light + x.1.start - x.0.start) - .unwrap_or(light); - let humidity = temp_to_humidity - .iter() - .find(|x| x.0.contains(&temp)) - .map(|x| temp + x.1.start - x.0.start) - .unwrap_or(temp); - let loc = humidity_to_loc - .iter() - .find(|x| x.0.contains(&humidity)) - .map(|x| humidity + x.1.start - x.0.start) - .unwrap_or(humidity); - - answer = std::cmp::min(answer, loc); + answer = std::cmp::min(answer, src); } answer