readying 2022 to merge
This commit is contained in:
7
2022/day4/Cargo.lock
generated
Normal file
7
2022/day4/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day4"
|
||||
version = "0.1.0"
|
||||
11
2022/day4/Cargo.toml
Normal file
11
2022/day4/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "day4"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
authors.workspace = true
|
||||
repository.workspace = true
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
nom.workspace = true
|
||||
1000
2022/day4/input
Normal file
1000
2022/day4/input
Normal file
File diff suppressed because it is too large
Load Diff
79
2022/day4/src/main.rs
Normal file
79
2022/day4/src/main.rs
Normal file
@@ -0,0 +1,79 @@
|
||||
#![warn(clippy::all, clippy::pedantic)]
|
||||
|
||||
use std::fs;
|
||||
|
||||
use nom::{
|
||||
bytes::complete::tag,
|
||||
character::complete::{self, newline},
|
||||
multi::separated_list0,
|
||||
sequence::separated_pair,
|
||||
};
|
||||
|
||||
fn parse_range(input: &str) -> nom::IResult<&str, (i32, i32)> {
|
||||
separated_pair(complete::i32, tag("-"), complete::i32)(input)
|
||||
}
|
||||
fn parse_line(input: &str) -> nom::IResult<&str, ((i32, i32), (i32, i32))> {
|
||||
separated_pair(parse_range, tag(","), parse_range)(input)
|
||||
}
|
||||
fn process_input(input: &str) -> nom::IResult<&str, Vec<((i32, i32), (i32, i32))>> {
|
||||
separated_list0(newline, parse_line)(input)
|
||||
}
|
||||
|
||||
fn part1(input: &str) -> String {
|
||||
do_part(input, |(a, b)| {
|
||||
i32::from((a.0 <= b.0 && a.1 >= b.1) || (a.0 >= b.0 && a.1 <= b.1))
|
||||
})
|
||||
}
|
||||
fn part2(input: &str) -> String {
|
||||
do_part(input, |(a, b)| {
|
||||
i32::from(
|
||||
(a.0 >= b.0 && a.0 <= b.1)
|
||||
|| (a.1 >= b.0 && a.1 <= b.1)
|
||||
|| (b.0 >= a.0 && b.0 <= a.1)
|
||||
|| (b.1 >= a.0 && b.1 <= a.1),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn do_part(input: &str, f: impl Fn((&(i32, i32), &(i32, i32))) -> i32) -> String {
|
||||
let (_, ranges) = process_input(input).unwrap();
|
||||
ranges
|
||||
.iter()
|
||||
.map(|(a, b)| f((a, b)))
|
||||
.fold(0, |mut acc: i32, x: i32| {
|
||||
acc += x;
|
||||
acc
|
||||
})
|
||||
.to_string()
|
||||
}
|
||||
fn main() -> std::io::Result<()> {
|
||||
//Read in file
|
||||
let file = fs::read_to_string("input")?;
|
||||
|
||||
println!("Part 1: {}", part1(&file));
|
||||
println!("Part 2: {}", part2(&file));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
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 part1_works() {
|
||||
assert_eq!(part1(INPUT), "2")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_works() {
|
||||
assert_eq!(part2(INPUT), "4")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user