Improved solution
This commit is contained in:
parent
e38950bc6e
commit
ec935fb2ef
|
@ -23,31 +23,48 @@ mod tests {
|
||||||
fn part1_solution() -> Result<()> {
|
fn part1_solution() -> Result<()> {
|
||||||
Day::test(aoc::Part::ONE, "input", 567)
|
Day::test(aoc::Part::ONE, "input", 567)
|
||||||
}
|
}
|
||||||
|
#[test]
|
||||||
|
fn part2_solution() -> Result<()> {
|
||||||
|
Day::test(aoc::Part::TWO, "input", 907)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// -- Helper --
|
// -- Implementation --
|
||||||
// @TODO Implement this on the iterator?
|
struct Elf {
|
||||||
fn apply<F, U, V>((a, b): (U, U), f: F) -> (V, V) where
|
start: u32,
|
||||||
F: Fn(U) -> V {
|
end: u32,
|
||||||
(f(a), f(b))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This filter check if either side is fully contained in the other side
|
impl Elf {
|
||||||
fn contains(pair: &((u32, u32), (u32, u32))) -> bool {
|
fn new(a: u32, b: u32) -> Elf {
|
||||||
(pair.0.0 <= pair.1.0 && pair.0.1 >= pair.1.1) || (pair.1.0 <= pair.0.0 && pair.1.1 >= pair.0.1)
|
Elf { start: a, end: b }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn overlaps(pair: &((u32, u32), (u32, u32))) -> bool {
|
// -- Helpers --
|
||||||
if pair.0.1 >= pair.1.0 && pair.0.1 <= pair.1.1 {
|
// Check if one of the elf is fully contained by the other
|
||||||
true
|
fn contains((a, b): &(Elf, Elf)) -> bool {
|
||||||
} else if pair.0.0 >= pair.1.0 && pair.0.0 <= pair.1.1 {
|
(a.start <= b.start && a.end >= b.end) || (b.start <= a.start && b.end >= a.end)
|
||||||
true
|
}
|
||||||
} else if pair.1.1 >= pair.0.0 && pair.1.1 <= pair.0.1 {
|
|
||||||
true
|
// Check if there is overlap between two elfs
|
||||||
} else if pair.1.0 >= pair.0.0 && pair.1.0 <= pair.0.1 {
|
fn overlaps((a, b): &(Elf, Elf)) -> bool {
|
||||||
true
|
cmp::min(a.end, b.end) >= cmp::max(a.start, b.start)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transform from line to pair of Elfs
|
||||||
|
fn transform(s: &str) -> (Elf, Elf) {
|
||||||
|
let transformed = s.split(',')
|
||||||
|
.flat_map(|pair| pair.split('-'))
|
||||||
|
.flat_map(|value| value.parse::<u32>())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
assert_eq!(transformed.len(), 4, "Invalid input");
|
||||||
|
|
||||||
|
if let [a, b, c, d] = transformed[..4] {
|
||||||
|
(Elf::new(a,b), Elf::new(c, d))
|
||||||
} else {
|
} else {
|
||||||
false
|
panic!("Invalid input")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,17 +78,7 @@ impl aoc::Solver for Day {
|
||||||
fn part1(input: &str) -> u32 {
|
fn part1(input: &str) -> u32 {
|
||||||
input
|
input
|
||||||
.lines()
|
.lines()
|
||||||
.map(|line| line.split_once(',').expect("Invalid input"))
|
.map(transform)
|
||||||
.map(|pair| apply(pair, |pair| pair.split_once("-").expect("Invalid input")))
|
|
||||||
.map(|pair| apply(pair, |side| {
|
|
||||||
apply(side, |a| {
|
|
||||||
if let Ok(num) = a.parse::<u32>() {
|
|
||||||
num
|
|
||||||
} else {
|
|
||||||
panic!("Invalid input")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}))
|
|
||||||
.filter(contains)
|
.filter(contains)
|
||||||
.count() as u32
|
.count() as u32
|
||||||
}
|
}
|
||||||
|
@ -79,17 +86,7 @@ impl aoc::Solver for Day {
|
||||||
fn part2(input: &str) -> u32 {
|
fn part2(input: &str) -> u32 {
|
||||||
input
|
input
|
||||||
.lines()
|
.lines()
|
||||||
.map(|line| line.split_once(',').expect("Invalid input"))
|
.map(transform)
|
||||||
.map(|pair| apply(pair, |pair| pair.split_once("-").expect("Invalid input")))
|
|
||||||
.map(|pair| apply(pair, |side| {
|
|
||||||
apply(side, |a| {
|
|
||||||
if let Ok(num) = a.parse::<u32>() {
|
|
||||||
num
|
|
||||||
} else {
|
|
||||||
panic!("Invalid input")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}))
|
|
||||||
.filter(overlaps)
|
.filter(overlaps)
|
||||||
.count() as u32
|
.count() as u32
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user