Rust Day 5 Part 2 replace RangeInclusive w/ Range

This commit is contained in:
Tobias Berger 2023-12-05 21:16:40 +01:00
parent 0ec05b7b34
commit 4c1245d7cc
Signed by: toby
GPG key ID: 2D05EFAB764D6A88

View file

@ -1,8 +1,8 @@
use std::{cmp::Ordering, fmt::Debug, ops::RangeInclusive, str::FromStr}; use std::{cmp::Ordering, fmt::Debug, ops::Range, str::FromStr};
pub(crate) struct Mapping { pub(crate) struct Mapping {
source_range: std::ops::RangeInclusive<i64>, source_range: std::ops::Range<i64>,
destination_range: std::ops::RangeInclusive<i64>, destination_range: std::ops::Range<i64>,
} }
impl Debug for Mapping { impl Debug for Mapping {
@ -22,7 +22,7 @@ enum Overlap {
// RANGE 789 // < < < < // 789 // X // RANGE 789 // < < < < // 789 // X
// RANGE 123 // > > > > // 123 // X // RANGE 123 // > > > > // 123 // X
None { None {
rest: RangeInclusive<i64>, rest: Range<i64>,
}, },
// MAPPING 345 -> 789 (+4) // MAPPING 345 -> 789 (+4)
// RANGE 234 // > > < > // 2 78 // X // RANGE 234 // > > < > // 2 78 // X
@ -32,63 +32,63 @@ enum Overlap {
// RANGE 1234 // > = < > // 12 78 // X // RANGE 1234 // > = < > // 12 78 // X
// RANGE 3456 // = < < > // 789 X // // RANGE 3456 // = < < > // 789 X //
Simple { Simple {
contained: RangeInclusive<i64>, contained: Range<i64>,
rest: RangeInclusive<i64>, rest: Range<i64>,
}, },
// MAPPING 1234 -> 5678 (+4) // MAPPING 1234 -> 5678 (+4)
// RANGE 234 // < = < > // 678 // X // RANGE 234 // < = < > // 678 // X
// RANGE 123 // = > < > // 567 // X // RANGE 123 // = > < > // 567 // X
Contained { Contained {
contained: RangeInclusive<i64>, contained: Range<i64>,
}, },
// MAPPING 12345 -> 56789 (+4) // MAPPING 12345 -> 56789 (+4)
// RANGE 234 // < > < > // 789 // X // RANGE 234 // < > < > // 789 // X
// RANGE 12345 // = = < > // 56789 // X // RANGE 12345 // = = < > // 56789 // X
Complete { Complete {
contained: RangeInclusive<i64>, contained: Range<i64>,
}, },
// MAPPING 234 -> 678 (+4) // MAPPING 234 -> 678 (+4)
// RANGE 12345 // > < < > // 1 678 5 // X // RANGE 12345 // > < < > // 1 678 5 // X
Overreaching { Overreaching {
left: RangeInclusive<i64>, left: Range<i64>,
contained: RangeInclusive<i64>, contained: Range<i64>,
right: RangeInclusive<i64>, right: Range<i64>,
}, },
} }
impl Mapping { impl Mapping {
const fn diff(&self) -> i64 { const fn diff(&self) -> i64 {
*self.destination_range.start() - *self.source_range.start() self.destination_range.start - self.source_range.start
} }
fn apply(&self, range: &RangeInclusive<i64>) -> Overlap { fn apply(&self, range: &Range<i64>) -> Overlap {
let result = match ( let result = match (
i64::cmp(self.source_range.start(), range.start()), i64::cmp(&self.source_range.start, &range.start),
i64::cmp(self.source_range.end(), range.end()), i64::cmp(&self.source_range.end, &range.end),
i64::cmp(self.source_range.start(), range.end()), i64::cmp(&self.source_range.start, &range.end),
i64::cmp(self.source_range.end(), range.start()), i64::cmp(&self.source_range.end, &range.start),
) { ) {
(Ordering::Equal, Ordering::Less, Ordering::Less, Ordering::Greater) => { (Ordering::Equal, Ordering::Less, Ordering::Less, Ordering::Greater) => {
Overlap::Simple { Overlap::Simple {
contained: self.destination_range.clone(), contained: self.destination_range.clone(),
rest: self.source_range.end() + 1..=*range.end(), rest: self.source_range.end + 1..range.end,
} }
} }
(Ordering::Greater, Ordering::Equal, Ordering::Less, Ordering::Greater) => { (Ordering::Greater, Ordering::Equal, Ordering::Less, Ordering::Greater) => {
Overlap::Simple { Overlap::Simple {
contained: *self.destination_range.start()..=range.end() + self.diff(), contained: self.destination_range.start..range.end + self.diff(),
rest: *range.start()..=self.source_range.start() - 1, rest: range.start..self.source_range.start - 1,
} }
} }
(Ordering::Greater, Ordering::Less, Ordering::Less, Ordering::Greater) => { (Ordering::Greater, Ordering::Less, Ordering::Less, Ordering::Greater) => {
Overlap::Overreaching { Overlap::Overreaching {
left: *range.start()..=self.source_range.start() - 1, left: range.start..self.source_range.start - 1,
contained: self.destination_range.clone(), contained: self.destination_range.clone(),
right: self.source_range.end() + 1..=*range.end(), right: self.source_range.end + 1..range.end,
} }
} }
(Ordering::Less, Ordering::Greater, Ordering::Less, Ordering::Greater) => { (Ordering::Less, Ordering::Greater, Ordering::Less, Ordering::Greater) => {
Overlap::Complete { Overlap::Complete {
contained: range.start() + self.diff()..=range.end() + self.diff(), contained: range.start + self.diff()..range.end + self.diff(),
} }
} }
(Ordering::Equal, Ordering::Equal, Ordering::Less, Ordering::Greater) => { (Ordering::Equal, Ordering::Equal, Ordering::Less, Ordering::Greater) => {
@ -103,26 +103,26 @@ impl Mapping {
(Ordering::Less, Ordering::Equal, Ordering::Less, Ordering::Greater) (Ordering::Less, Ordering::Equal, Ordering::Less, Ordering::Greater)
| (Ordering::Equal, Ordering::Greater, Ordering::Less, Ordering::Greater) => { | (Ordering::Equal, Ordering::Greater, Ordering::Less, Ordering::Greater) => {
Overlap::Contained { Overlap::Contained {
contained: range.start() + self.diff()..=range.end() + self.diff(), contained: range.start + self.diff()..range.end + self.diff(),
} }
} }
(Ordering::Greater, Ordering::Greater, Ordering::Less, Ordering::Greater) => { (Ordering::Greater, Ordering::Greater, Ordering::Less, Ordering::Greater) => {
Overlap::Simple { Overlap::Simple {
rest: *range.start()..=self.source_range.start() - 1, rest: range.start..self.source_range.start - 1,
contained: *self.destination_range.start()..=range.end() + self.diff(), contained: self.destination_range.start..range.end + self.diff(),
} }
} }
(Ordering::Less, Ordering::Less, Ordering::Less, Ordering::Greater) (Ordering::Less, Ordering::Less, Ordering::Less, Ordering::Greater)
| (Ordering::Less, Ordering::Less, Ordering::Less, Ordering::Equal) => { | (Ordering::Less, Ordering::Less, Ordering::Less, Ordering::Equal) => {
Overlap::Simple { Overlap::Simple {
contained: *range.start() + self.diff()..=*self.destination_range.end(), contained: range.start + self.diff()..self.destination_range.end,
rest: self.source_range.end() + 1..=*range.end(), rest: self.source_range.end + 1..range.end,
} }
} }
(Ordering::Greater, Ordering::Greater, Ordering::Equal, Ordering::Greater) => { (Ordering::Greater, Ordering::Greater, Ordering::Equal, Ordering::Greater) => {
Overlap::Simple { Overlap::Simple {
rest: *range.start()..=self.source_range.start() - 1, rest: range.start..self.source_range.start - 1,
contained: *self.destination_range.start()..=range.end() + self.diff(), contained: self.destination_range.start..range.end + self.diff(),
} }
} }
(a, b, c, d) => unreachable!("{:?} {:?} {:?} {:?}", a, b, c, d), (a, b, c, d) => unreachable!("{:?} {:?} {:?} {:?}", a, b, c, d),
@ -130,20 +130,19 @@ impl Mapping {
match &result { match &result {
Overlap::Contained { contained } => assert_eq!( Overlap::Contained { contained } => assert_eq!(
range.end() - range.start() + 1, range.end - range.start + 1,
contained.end() - contained.start() + 1 contained.end - contained.start + 1
), ),
Overlap::Complete { contained } => assert_eq!( Overlap::Complete { contained } => assert_eq!(
range.end() - range.start() + 1, range.end - range.start + 1,
contained.end() - contained.start() + 1 contained.end - contained.start + 1
),
Overlap::None { rest } => assert_eq!(
range.end() - range.start() + 1,
rest.end() - rest.start() + 1
), ),
Overlap::None { rest } => {
assert_eq!(range.end - range.start + 1, rest.end - rest.start + 1)
}
Overlap::Simple { rest, contained } => assert_eq!( Overlap::Simple { rest, contained } => assert_eq!(
range.end() - range.start() + 1, range.end - range.start + 1,
(rest.end() - rest.start() + 1) + (contained.end() - contained.start() + 1), (rest.end - rest.start + 1) + (contained.end - contained.start + 1),
"{:?} ; {} ; {:?} ; {:?}", "{:?} ; {} ; {:?} ; {:?}",
range, range,
self, self,
@ -156,10 +155,10 @@ impl Mapping {
contained, contained,
right, right,
} => assert_eq!( } => assert_eq!(
range.end() - range.start() + 1, range.end - range.start + 1,
(left.end() - left.start() + 1) (left.end - left.start + 1)
+ (contained.end() - contained.start() + 1) + (contained.end - contained.start + 1)
+ (right.end() - right.start() + 1) + (right.end - right.start + 1)
), ),
} }
@ -179,8 +178,8 @@ impl FromStr for Mapping {
let source_start = range[1]; let source_start = range[1];
let length = range[2]; let length = range[2];
Ok(Mapping { Ok(Mapping {
destination_range: destination_start..=destination_start + length - 1, destination_range: destination_start..destination_start + length - 1,
source_range: source_start..=source_start + length - 1, source_range: source_start..source_start + length - 1,
}) })
} }
} }
@ -196,7 +195,7 @@ pub(crate) fn part_2(input: &'static str) -> Result<i64, <Mapping as FromStr>::E
.collect::<Result<Vec<_>, _>>()? .collect::<Result<Vec<_>, _>>()?
.chunks_exact(2) .chunks_exact(2)
.map(|v| [v[0], v[1]]) .map(|v| [v[0], v[1]])
.map(|[start, length]| start..=(start + length - 1)) .map(|[start, length]| start..(start + length - 1))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
println!("seed {:?}", seed_values); println!("seed {:?}", seed_values);
let seed_to_soil = lines let seed_to_soil = lines
@ -471,12 +470,7 @@ pub(crate) fn part_2(input: &'static str) -> Result<i64, <Mapping as FromStr>::E
location_values.extend_from_slice(&humidity_values); location_values.extend_from_slice(&humidity_values);
println!("loct {:?}", location_values); println!("loct {:?}", location_values);
Ok(location_values Ok(location_values.iter().map(|v| v.start).min().unwrap())
.iter()
.map(RangeInclusive::start)
.min()
.copied()
.unwrap())
} }
#[cfg(test)] #[cfg(test)]