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() .unwrap()
.collect(); .collect();
let seed_to_soil: Vec<(Range<u64>, Range<u64>)> = lines let maps: Vec<Vec<(Range<u64>, Range<u64>)>> = lines
.next() .map(|lines| {
.map(|x| { lines
x.lines().skip(1).map(|line| { .lines()
let y: Vec<u64> = line.split(' ').map(|y| y.parse::<u64>().unwrap()).collect(); .skip(1)
let size = y[2]; .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(); .collect();
let mut answer = std::u64::MAX; let mut answer = std::u64::MAX;
for seed in seeds { for mut src in seeds {
let soil = seed_to_soil for map in maps.iter() {
.iter() let dst = map
.find(|x| x.0.contains(&seed)) .iter()
.map(|x| seed + x.1.start - x.0.start) .find(|x| x.0.contains(&src))
.unwrap_or(seed); .map(|x| src + x.1.start - x.0.start)
.unwrap_or(src);
let ferti = soil_to_ferti src = dst;
.iter() }
.find(|x| x.0.contains(&soil))
.map(|x| soil + x.1.start - x.0.start)
.unwrap_or(soil);
let water = ferti_to_water answer = std::cmp::min(answer, src);
.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 answer

View File

@ -25,140 +25,38 @@ fn process(data: &str) -> u64 {
}) })
.unwrap(); .unwrap();
let seed_to_soil: Vec<(Range<u64>, Range<u64>)> = lines let maps: Vec<Vec<(Range<u64>, Range<u64>)>> = lines
.next() .map(|lines| {
.map(|x| { lines
x.lines().skip(1).map(|line| { .lines()
let y: Vec<u64> = line.split(' ').map(|y| y.parse::<u64>().unwrap()).collect(); .skip(1)
let size = y[2]; .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(); .collect();
let mut answer = std::u64::MAX; let mut answer = std::u64::MAX;
for seed in seeds { for mut src in seeds {
let soil = seed_to_soil for map in maps.iter() {
.iter() let dst = map
.find(|x| x.0.contains(&seed)) .iter()
.map(|x| seed + x.1.start - x.0.start) .find(|x| x.0.contains(&src))
.unwrap_or(seed); .map(|x| src + x.1.start - x.0.start)
.unwrap_or(src);
let ferti = soil_to_ferti src = dst;
.iter() }
.find(|x| x.0.contains(&soil))
.map(|x| soil + x.1.start - x.0.start)
.unwrap_or(soil);
let water = ferti_to_water answer = std::cmp::min(answer, src);
.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 answer