2022 - Day 4
This commit is contained in:
parent
89a62bf7b3
commit
e38950bc6e
1000
2022/input/4/input
Normal file
1000
2022/input/4/input
Normal file
File diff suppressed because it is too large
Load Diff
6
2022/input/4/test-1
Normal file
6
2022/input/4/test-1
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
2-4,6-8
|
||||||
|
2-3,4-5
|
||||||
|
5-7,7-9
|
||||||
|
2-8,3-7
|
||||||
|
6-6,4-6
|
||||||
|
2-6,4-8
|
96
2022/src/bin/day4.rs
Normal file
96
2022/src/bin/day4.rs
Normal file
|
@ -0,0 +1,96 @@
|
||||||
|
use std::cmp;
|
||||||
|
use anyhow::Result;
|
||||||
|
use aoc::Solver;
|
||||||
|
|
||||||
|
// -- Runners --
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
Day::solve()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part1_test1() -> Result<()> {
|
||||||
|
Day::test(aoc::Part::ONE, "test-1", 2)
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn part2_test1() -> Result<()> {
|
||||||
|
Day::test(aoc::Part::TWO, "test-1", 4)
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn part1_solution() -> Result<()> {
|
||||||
|
Day::test(aoc::Part::ONE, "input", 567)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// -- Helper --
|
||||||
|
// @TODO Implement this on the iterator?
|
||||||
|
fn apply<F, U, V>((a, b): (U, U), f: F) -> (V, V) where
|
||||||
|
F: Fn(U) -> V {
|
||||||
|
(f(a), f(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
// This filter check if either side is fully contained in the other side
|
||||||
|
fn contains(pair: &((u32, u32), (u32, u32))) -> bool {
|
||||||
|
(pair.0.0 <= pair.1.0 && pair.0.1 >= pair.1.1) || (pair.1.0 <= pair.0.0 && pair.1.1 >= pair.0.1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn overlaps(pair: &((u32, u32), (u32, u32))) -> bool {
|
||||||
|
if pair.0.1 >= pair.1.0 && pair.0.1 <= pair.1.1 {
|
||||||
|
true
|
||||||
|
} else if pair.0.0 >= pair.1.0 && pair.0.0 <= pair.1.1 {
|
||||||
|
true
|
||||||
|
} else if pair.1.1 >= pair.0.0 && pair.1.1 <= pair.0.1 {
|
||||||
|
true
|
||||||
|
} else if pair.1.0 >= pair.0.0 && pair.1.0 <= pair.0.1 {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// -- Solution --
|
||||||
|
pub struct Day;
|
||||||
|
impl aoc::Solver for Day {
|
||||||
|
fn day() -> u8 {
|
||||||
|
4
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part1(input: &str) -> u32 {
|
||||||
|
input
|
||||||
|
.lines()
|
||||||
|
.map(|line| line.split_once(',').expect("Invalid input"))
|
||||||
|
.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)
|
||||||
|
.count() as u32
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part2(input: &str) -> u32 {
|
||||||
|
input
|
||||||
|
.lines()
|
||||||
|
.map(|line| line.split_once(',').expect("Invalid input"))
|
||||||
|
.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)
|
||||||
|
.count() as u32
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user