mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-27 13:50:32 +01:00
Compare commits
3 Commits
e1b3b9d179
...
2022/4-ran
| Author | SHA1 | Date | |
|---|---|---|---|
| f904d050cc | |||
| 6802a7bf33 | |||
| 9d23e80256 |
@@ -8,7 +8,7 @@ use criterion::BenchmarkId;
|
||||
use criterion::Criterion;
|
||||
|
||||
/// 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> {
|
||||
let input_path = format!("inputs/{:02}.txt", day);
|
||||
|
||||
1000
2022/inputs/04.txt
Normal file
1000
2022/inputs/04.txt
Normal file
File diff suppressed because it is too large
Load Diff
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,75 @@
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
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> {
|
||||
todo!()
|
||||
use crate::common::parse_input;
|
||||
|
||||
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> {
|
||||
todo!()
|
||||
fn is_contained(a: &Assignment, b: &Assignment) -> bool {
|
||||
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
6
2022/src/samples/04.txt
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
|
||||
Reference in New Issue
Block a user