3 Commits

Author SHA1 Message Date
f904d050cc Reimplement day 4 2022 with ranges instead of ints 2022-12-06 07:55:54 +01:00
6802a7bf33 Refactor common parts 2022-12-04 11:18:03 +01:00
9d23e80256 Implement 2022 day 4 2022-12-04 11:14:23 +01:00
5 changed files with 1089 additions and 5 deletions

View File

@@ -8,7 +8,7 @@ use criterion::BenchmarkId;
use criterion::Criterion; use criterion::Criterion;
/// Number of days we have an implementation to benchmark /// Number of days we have an implementation to benchmark
const DAYS_IMPLEMENTED: u8 = 3; const DAYS_IMPLEMENTED: u8 = 4;
fn read_input(day: u8) -> Vec<u8> { fn read_input(day: u8) -> Vec<u8> {
let input_path = format!("inputs/{:02}.txt", day); let input_path = format!("inputs/{:02}.txt", day);

1000
2022/inputs/04.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -92,3 +92,15 @@ where
} }
} }
} }
/// Return the minimum and maximum of two unordered variables
pub fn minmax<T>(a: T, b: T) -> (T, T)
where
T: PartialOrd,
{
if a < b {
(a, b)
} else {
(b, a)
}
}

View File

@@ -1,9 +1,75 @@
use std::ops::RangeInclusive;
use anyhow::Result; use anyhow::Result;
use nom::bytes::complete::tag;
use nom::character::complete::newline;
use nom::combinator::map;
use nom::multi::many0;
use nom::sequence::separated_pair;
use nom::sequence::terminated;
use nom::IResult;
pub fn part1(_input: &[u8]) -> Result<String> { use crate::common::parse_input;
todo!()
type Assignment = RangeInclusive<u32>;
fn parse_assignments(
input: &[u8],
) -> IResult<&[u8], Vec<(RangeInclusive<u32>, RangeInclusive<u32>)>> {
use nom::character::complete::u32;
fn parse_single(input: &[u8]) -> IResult<&[u8], Assignment> {
map(separated_pair(u32, tag("-"), u32), |(start, end)| {
start..=end
})(input)
}
let parse_line = separated_pair(parse_single, tag(","), parse_single);
many0(terminated(parse_line, newline))(input)
} }
pub fn part2(_input: &[u8]) -> Result<String> { fn is_contained(a: &Assignment, b: &Assignment) -> bool {
todo!() if a.size_hint().0 > b.size_hint().0 {
a.contains(b.start()) && a.contains(b.end())
} else {
b.contains(a.start()) && b.contains(a.end())
}
}
fn is_overlapping(a: &Assignment, b: &Assignment) -> bool {
b.end() >= a.start() && b.start() <= a.end() || a.end() >= b.start() && a.start() <= b.end()
}
fn parts_common(input: &[u8], filter: impl Fn(&Assignment, &Assignment) -> bool) -> Result<String> {
let assigments = parse_input(input, parse_assignments)?;
let overlapping = assigments.into_iter().filter(|(a, b)| filter(a, b)).count();
Ok(overlapping.to_string())
}
pub fn part1(input: &[u8]) -> Result<String> {
parts_common(input, is_contained)
}
pub fn part2(input: &[u8]) -> Result<String> {
parts_common(input, is_overlapping)
}
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE: &[u8] = include_bytes!("samples/04.txt");
#[test]
fn sample_part1() {
assert_eq!(part1(SAMPLE).unwrap(), "2")
}
#[test]
fn sample_part2() {
assert_eq!(part2(SAMPLE).unwrap(), "4")
}
} }

6
2022/src/samples/04.txt Normal file
View 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