Add second approach using reverse search
This commit is contained in:
@@ -109,6 +109,15 @@ impl Map {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn map_reverse(&self, value: usize) -> Option<usize> {
|
||||||
|
if self.destination_range.contains(&value) {
|
||||||
|
let index = value - self.destination_range.start;
|
||||||
|
Some(self.source_range.clone().nth(index).unwrap())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -142,6 +151,15 @@ impl MapList {
|
|||||||
}
|
}
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn map_reverse(&self, value: usize) -> usize {
|
||||||
|
for map in &self.maps {
|
||||||
|
if let Some(mapped_value) = map.map_reverse(value) {
|
||||||
|
return mapped_value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part1(input: &str) -> Result<usize, String> {
|
fn part1(input: &str) -> Result<usize, String> {
|
||||||
@@ -169,29 +187,60 @@ fn part2(input: &str) -> Result<usize, String> {
|
|||||||
println!("parsing rest found: {rest}");
|
println!("parsing rest found: {rest}");
|
||||||
panic!();
|
panic!();
|
||||||
}
|
}
|
||||||
let lowest_location = almanac
|
|
||||||
.seeds
|
#[derive(PartialEq, Eq)]
|
||||||
.0
|
#[allow(dead_code)]
|
||||||
.into_par_iter()
|
enum Approach {
|
||||||
.map(|seed_range| {
|
Reverse,
|
||||||
println!("{seed_range:?}");
|
BruteForce,
|
||||||
// let seeds = seed_range.collect::<Vec<usize>>();
|
}
|
||||||
let result = seed_range
|
|
||||||
.clone()
|
let approach = Approach::BruteForce;
|
||||||
.map(|seed| {
|
|
||||||
let mut mapped_value = seed;
|
let lowest_location = match approach {
|
||||||
for map_list in &almanac.map_lists {
|
Approach::Reverse => {
|
||||||
mapped_value = map_list.map(mapped_value);
|
let mut i = 0;
|
||||||
}
|
loop {
|
||||||
mapped_value
|
let mut reverse_val = i;
|
||||||
})
|
for map_list in almanac.map_lists.iter().rev() {
|
||||||
.min()
|
reverse_val = map_list.map_reverse(reverse_val);
|
||||||
.unwrap();
|
}
|
||||||
println!("{seed_range:?} => {result}");
|
if almanac
|
||||||
result
|
.seeds
|
||||||
})
|
.0
|
||||||
.min()
|
.iter()
|
||||||
.unwrap();
|
.any(|seed| seed.contains(&reverse_val))
|
||||||
|
{
|
||||||
|
break i;
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Approach::BruteForce => almanac
|
||||||
|
.seeds
|
||||||
|
.0
|
||||||
|
.into_par_iter()
|
||||||
|
.map(|seed_range| {
|
||||||
|
println!("{seed_range:?}");
|
||||||
|
// let seeds = seed_range.collect::<Vec<usize>>();
|
||||||
|
let result = seed_range
|
||||||
|
.clone()
|
||||||
|
.map(|seed| {
|
||||||
|
let mut mapped_value = seed;
|
||||||
|
for map_list in &almanac.map_lists {
|
||||||
|
mapped_value = map_list.map(mapped_value);
|
||||||
|
}
|
||||||
|
mapped_value
|
||||||
|
})
|
||||||
|
.min()
|
||||||
|
.unwrap();
|
||||||
|
println!("{seed_range:?} => {result}");
|
||||||
|
result
|
||||||
|
})
|
||||||
|
.min()
|
||||||
|
.unwrap(),
|
||||||
|
};
|
||||||
|
|
||||||
Ok(lowest_location)
|
Ok(lowest_location)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user