This commit is contained in:
2022-12-04 14:51:24 +01:00
parent 726f75d327
commit a1c99e8268
7 changed files with 1115 additions and 0 deletions

25
Cargo.lock generated
View File

@@ -5,3 +5,28 @@ version = 3
[[package]]
name = "aoc2022"
version = "0.1.0"
dependencies = [
"nom",
]
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "minimal-lexical"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "nom"
version = "7.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8903e5a29a317527874d0402f867152a3d21c908bb0b933e416c65e301d4c36"
dependencies = [
"memchr",
"minimal-lexical",
]

View File

@@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
nom = "7.1.1"

1000
inputs/day4.txt Normal file

File diff suppressed because it is too large Load Diff

7
src/bin/day04_1.rs Normal file
View File

@@ -0,0 +1,7 @@
use aoc2022::day04::process_part_1;
use std::fs;
fn main() {
let file = fs::read_to_string("./inputs/day4.txt").unwrap();
println!("{}", process_part_1(&file));
}

7
src/bin/day04_2.rs Normal file
View File

@@ -0,0 +1,7 @@
use aoc2022::day04::process_part_2;
use std::fs;
fn main() {
let file = fs::read_to_string("./inputs/day4.txt").unwrap();
println!("{}", process_part_2(&file));
}

74
src/day04.rs Normal file
View File

@@ -0,0 +1,74 @@
use nom::IResult;
use std::ops::RangeInclusive;
pub fn process_part_1(input: &str) -> usize {
let (_, assignments) = section_assignments(input).unwrap();
assignments
.iter()
.filter(|(a, b)| range_contains(a, b) || range_contains(b, a))
.count()
}
pub fn process_part_2(input: &str) -> usize {
let (_, assignments) = section_assignments(input).unwrap();
assignments
.iter()
.filter(|(a, b)| a.clone().into_iter().any(|n| b.contains(&n)))
.count()
}
fn range_contains(haystack: &RangeInclusive<u32>, needle: &RangeInclusive<u32>) -> bool {
haystack.contains(needle.start()) && haystack.contains(needle.end())
}
fn section_assignments(
input: &str,
) -> IResult<&str, Vec<(RangeInclusive<u32>, RangeInclusive<u32>)>> {
use nom::character::complete::newline;
use nom::multi::separated_list1;
let (input, ranges) = separated_list1(newline, line)(input)?;
Ok((input, ranges))
}
fn line(input: &str) -> IResult<&str, (RangeInclusive<u32>, RangeInclusive<u32>)> {
use nom::bytes::complete::tag;
let (input, start) = sections(input)?;
let (input, _) = tag(",")(input)?;
let (input, end) = sections(input)?;
Ok((input, (start, end)))
}
fn sections(input: &str) -> IResult<&str, RangeInclusive<u32>> {
use nom::bytes::complete::tag;
use nom::character::complete;
let (input, start) = complete::u32(input)?;
let (input, _) = tag("-")(input)?;
let (input, end) = complete::u32(input)?;
Ok((input, start..=end))
}
#[cfg(test)]
mod tests {
use super::*;
const INPUT: &str = "2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8";
#[test]
fn day1() {
assert_eq!(process_part_1(INPUT), 2);
}
#[test]
fn day2() {
assert_eq!(process_part_2(INPUT), 4);
}
}

View File

@@ -4,3 +4,4 @@ extern crate core;
pub mod day01;
pub mod day02;
pub mod day03;
pub mod day04;