generated from janezicmatej/aoc-template
wip: solve with range type
This commit is contained in:
parent
00000140d3
commit
0000015049
|
@ -1,17 +1,32 @@
|
||||||
use std::{
|
use std::{
|
||||||
cmp::{max, min},
|
cmp::{max, min},
|
||||||
|
ops::Range,
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
use aoc::parsers::to_vec;
|
use aoc::parsers::to_vec;
|
||||||
|
|
||||||
type PairRange = (u64, u64);
|
trait RangeExt {
|
||||||
|
fn overlaps(&self, other: &Self) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> RangeExt for Range<T>
|
||||||
|
where
|
||||||
|
T: PartialOrd,
|
||||||
|
{
|
||||||
|
fn overlaps(&self, other: &Self) -> bool {
|
||||||
|
self.contains(&other.start) || self.contains(&other.end)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_range(start: u64, range: u64) -> Range<u64> {
|
||||||
|
start..(start + range)
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Mapping {
|
struct Mapping {
|
||||||
destination: u64,
|
destination: Range<u64>,
|
||||||
source: u64,
|
source: Range<u64>,
|
||||||
range: u64,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ParseMappingError;
|
struct ParseMappingError;
|
||||||
|
@ -22,34 +37,26 @@ impl FromStr for Mapping {
|
||||||
let nums: Vec<u64> = to_vec(s, ' ');
|
let nums: Vec<u64> = to_vec(s, ' ');
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
destination: nums[0],
|
destination: build_range(nums[2], nums[0]),
|
||||||
source: nums[1],
|
source: build_range(nums[1], nums[0]),
|
||||||
range: nums[2],
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Mapping {
|
impl Mapping {
|
||||||
fn contains(&self, n: u64) -> bool {
|
|
||||||
n >= self.source && n < self.source + self.range
|
|
||||||
}
|
|
||||||
|
|
||||||
fn contains_any(&self, (s, r): PairRange) -> bool {
|
|
||||||
s < self.source + self.range && s + r > self.source
|
|
||||||
}
|
|
||||||
|
|
||||||
fn map(&self, n: u64) -> u64 {
|
fn map(&self, n: u64) -> u64 {
|
||||||
let shift = n - self.source;
|
let shift = n - self.source.start;
|
||||||
self.destination + shift
|
self.destination.start + shift
|
||||||
}
|
}
|
||||||
|
|
||||||
fn split_range(&self, (s, r): PairRange) -> [Option<PairRange>; 3] {
|
fn split_range(&self, r: Range<u64>) -> [Option<Range<u64>>; 3] {
|
||||||
let mut fences = [
|
let mut fences = [
|
||||||
s,
|
r.start,
|
||||||
s + r,
|
r.end,
|
||||||
max(self.source, s),
|
max(self.source.start, r.start),
|
||||||
min(self.source + self.range, s + r),
|
min(self.source.end, r.end),
|
||||||
];
|
];
|
||||||
|
|
||||||
fences.sort();
|
fences.sort();
|
||||||
|
|
||||||
let mut v = Vec::new();
|
let mut v = Vec::new();
|
||||||
|
@ -58,13 +65,13 @@ impl Mapping {
|
||||||
let f = fences[i];
|
let f = fences[i];
|
||||||
let nf = fences[i + 1];
|
let nf = fences[i + 1];
|
||||||
if f != nf {
|
if f != nf {
|
||||||
v.push(Some((f, nf - f)))
|
v.push(Some(f..(nf - f)))
|
||||||
} else {
|
} else {
|
||||||
v.push(None)
|
v.push(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
v[..3].try_into().unwrap()
|
[v[0], v[1], v[2]]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue