day5: cleanup

This commit is contained in:
Ishan Jain 2023-12-05 11:51:29 +05:30
parent 6a830b5cb8
commit 75841ecc80
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
2 changed files with 48 additions and 252 deletions

View File

@ -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];
(y[1]..y[1] + size, y[0]..y[0] + size)
})
})
.unwrap()
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 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)
})
.collect()
})
.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
for mut src in seeds {
for map in maps.iter() {
let dst = map
.iter()
.find(|x| x.0.contains(&seed))
.map(|x| seed + x.1.start - x.0.start)
.unwrap_or(seed);
.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

View File

@ -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];
(y[1]..y[1] + size, y[0]..y[0] + size)
})
})
.unwrap()
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 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)
})
.collect()
})
.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
for mut src in seeds {
for map in maps.iter() {
let dst = map
.iter()
.find(|x| x.0.contains(&seed))
.map(|x| seed + x.1.start - x.0.start)
.unwrap_or(seed);
.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