day5: cleanup
This commit is contained in:
parent
6a830b5cb8
commit
75841ecc80
150
src/day5/1.rs
150
src/day5/1.rs
|
@ -16,140 +16,38 @@ fn process(data: &str) -> u64 {
|
|||
.unwrap()
|
||||
.collect();
|
||||
|
||||
let seed_to_soil: Vec<(Range<u64>, Range<u64>)> = lines
|
||||
.next()
|
||||
.map(|x| {
|
||||
x.lines().skip(1).map(|line| {
|
||||
let y: Vec<u64> = line.split(' ').map(|y| y.parse::<u64>().unwrap()).collect();
|
||||
let size = y[2];
|
||||
let maps: Vec<Vec<(Range<u64>, Range<u64>)>> = lines
|
||||
.map(|lines| {
|
||||
lines
|
||||
.lines()
|
||||
.skip(1)
|
||||
.map(|line| {
|
||||
let y: Vec<u64> = line
|
||||
.split_ascii_whitespace()
|
||||
.map(|y| y.parse::<u64>().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<u64>, Range<u64>)> = lines
|
||||
.next()
|
||||
.map(|x| {
|
||||
x.lines().skip(1).map(|line| {
|
||||
let y: Vec<u64> = line.split(' ').map(|y| y.parse::<u64>().unwrap()).collect();
|
||||
let size = y[2];
|
||||
|
||||
(y[1]..y[1] + size, y[0]..y[0] + size)
|
||||
})
|
||||
})
|
||||
.unwrap()
|
||||
.collect();
|
||||
|
||||
let ferti_to_water: Vec<(Range<u64>, Range<u64>)> = lines
|
||||
.next()
|
||||
.map(|x| {
|
||||
x.lines().skip(1).map(|line| {
|
||||
let y: Vec<u64> = line.split(' ').map(|y| y.parse::<u64>().unwrap()).collect();
|
||||
let size = y[2];
|
||||
|
||||
(y[1]..y[1] + size, y[0]..y[0] + size)
|
||||
})
|
||||
})
|
||||
.unwrap()
|
||||
.collect();
|
||||
|
||||
let water_to_light: Vec<(Range<u64>, Range<u64>)> = lines
|
||||
.next()
|
||||
.map(|x| {
|
||||
x.lines().skip(1).map(|line| {
|
||||
let y: Vec<u64> = line.split(' ').map(|y| y.parse::<u64>().unwrap()).collect();
|
||||
let size = y[2];
|
||||
|
||||
(y[1]..y[1] + size, y[0]..y[0] + size)
|
||||
})
|
||||
})
|
||||
.unwrap()
|
||||
.collect();
|
||||
|
||||
let light_to_temp: Vec<(Range<u64>, Range<u64>)> = lines
|
||||
.next()
|
||||
.map(|x| {
|
||||
x.lines().skip(1).map(|line| {
|
||||
let y: Vec<u64> = line.split(' ').map(|y| y.parse::<u64>().unwrap()).collect();
|
||||
let size = y[2];
|
||||
|
||||
(y[1]..y[1] + size, y[0]..y[0] + size)
|
||||
})
|
||||
})
|
||||
.unwrap()
|
||||
.collect();
|
||||
|
||||
let temp_to_humidity: Vec<(Range<u64>, Range<u64>)> = lines
|
||||
.next()
|
||||
.map(|x| {
|
||||
x.lines().skip(1).map(|line| {
|
||||
let y: Vec<u64> = line.split(' ').map(|y| y.parse::<u64>().unwrap()).collect();
|
||||
let size = y[2];
|
||||
|
||||
(y[1]..y[1] + size, y[0]..y[0] + size)
|
||||
})
|
||||
})
|
||||
.unwrap()
|
||||
.collect();
|
||||
let humidity_to_loc: Vec<(Range<u64>, Range<u64>)> = lines
|
||||
.next()
|
||||
.map(|x| {
|
||||
x.lines().skip(1).map(|line| {
|
||||
let y: Vec<u64> = line.split(' ').map(|y| y.parse::<u64>().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
|
||||
|
|
150
src/day5/2.rs
150
src/day5/2.rs
|
@ -25,140 +25,38 @@ fn process(data: &str) -> u64 {
|
|||
})
|
||||
.unwrap();
|
||||
|
||||
let seed_to_soil: Vec<(Range<u64>, Range<u64>)> = lines
|
||||
.next()
|
||||
.map(|x| {
|
||||
x.lines().skip(1).map(|line| {
|
||||
let y: Vec<u64> = line.split(' ').map(|y| y.parse::<u64>().unwrap()).collect();
|
||||
let size = y[2];
|
||||
let maps: Vec<Vec<(Range<u64>, Range<u64>)>> = lines
|
||||
.map(|lines| {
|
||||
lines
|
||||
.lines()
|
||||
.skip(1)
|
||||
.map(|line| {
|
||||
let y: Vec<u64> = line
|
||||
.split_ascii_whitespace()
|
||||
.map(|y| y.parse::<u64>().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<u64>, Range<u64>)> = lines
|
||||
.next()
|
||||
.map(|x| {
|
||||
x.lines().skip(1).map(|line| {
|
||||
let y: Vec<u64> = line.split(' ').map(|y| y.parse::<u64>().unwrap()).collect();
|
||||
let size = y[2];
|
||||
|
||||
(y[1]..y[1] + size, y[0]..y[0] + size)
|
||||
})
|
||||
})
|
||||
.unwrap()
|
||||
.collect();
|
||||
|
||||
let ferti_to_water: Vec<(Range<u64>, Range<u64>)> = lines
|
||||
.next()
|
||||
.map(|x| {
|
||||
x.lines().skip(1).map(|line| {
|
||||
let y: Vec<u64> = line.split(' ').map(|y| y.parse::<u64>().unwrap()).collect();
|
||||
let size = y[2];
|
||||
|
||||
(y[1]..y[1] + size, y[0]..y[0] + size)
|
||||
})
|
||||
})
|
||||
.unwrap()
|
||||
.collect();
|
||||
|
||||
let water_to_light: Vec<(Range<u64>, Range<u64>)> = lines
|
||||
.next()
|
||||
.map(|x| {
|
||||
x.lines().skip(1).map(|line| {
|
||||
let y: Vec<u64> = line.split(' ').map(|y| y.parse::<u64>().unwrap()).collect();
|
||||
let size = y[2];
|
||||
|
||||
(y[1]..y[1] + size, y[0]..y[0] + size)
|
||||
})
|
||||
})
|
||||
.unwrap()
|
||||
.collect();
|
||||
|
||||
let light_to_temp: Vec<(Range<u64>, Range<u64>)> = lines
|
||||
.next()
|
||||
.map(|x| {
|
||||
x.lines().skip(1).map(|line| {
|
||||
let y: Vec<u64> = line.split(' ').map(|y| y.parse::<u64>().unwrap()).collect();
|
||||
let size = y[2];
|
||||
|
||||
(y[1]..y[1] + size, y[0]..y[0] + size)
|
||||
})
|
||||
})
|
||||
.unwrap()
|
||||
.collect();
|
||||
|
||||
let temp_to_humidity: Vec<(Range<u64>, Range<u64>)> = lines
|
||||
.next()
|
||||
.map(|x| {
|
||||
x.lines().skip(1).map(|line| {
|
||||
let y: Vec<u64> = line.split(' ').map(|y| y.parse::<u64>().unwrap()).collect();
|
||||
let size = y[2];
|
||||
|
||||
(y[1]..y[1] + size, y[0]..y[0] + size)
|
||||
})
|
||||
})
|
||||
.unwrap()
|
||||
.collect();
|
||||
let humidity_to_loc: Vec<(Range<u64>, Range<u64>)> = lines
|
||||
.next()
|
||||
.map(|x| {
|
||||
x.lines().skip(1).map(|line| {
|
||||
let y: Vec<u64> = line.split(' ').map(|y| y.parse::<u64>().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
|
||||
|
|
Loading…
Reference in New Issue
Block a user