Add solution for 2022 day 04
This commit is contained in:
parent
9c592395bf
commit
5878d30713
26
2022/Cargo.lock
generated
26
2022/Cargo.lock
generated
@ -9,3 +9,29 @@ version = "0.1.0"
|
||||
[[package]]
|
||||
name = "aoc-2022-02"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "aoc-2022-03"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"itertools",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aoc-2022-04"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.10.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
@ -1,5 +1,5 @@
|
||||
[workspace]
|
||||
members = ["./day-01", "./day-02"]
|
||||
members = ["./day-01", "./day-02", "./day-03", "./day-04"]
|
||||
|
||||
|
||||
[workspace.package]
|
||||
|
@ -7,7 +7,7 @@
|
||||
| 01 | ✓ | ✓ | [Rust] |
|
||||
| 02 | ✓ | ✓ | [Rust] |
|
||||
| 03 | ✓ | ✓ | [Rust] |
|
||||
| 04 | | | |
|
||||
| 04 | ✓ | ✓ | [Rust] |
|
||||
| 05 | | | |
|
||||
| 06 | | | |
|
||||
| 07 | | | |
|
||||
|
6
2022/day-04/Cargo.toml
Normal file
6
2022/day-04/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "aoc-2022-04"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
1000
2022/day-04/inputs/puzzle.txt
Normal file
1000
2022/day-04/inputs/puzzle.txt
Normal file
File diff suppressed because it is too large
Load Diff
6
2022/day-04/inputs/test.txt
Normal file
6
2022/day-04/inputs/test.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
|
18
2022/day-04/src/bin/part_one.rs
Normal file
18
2022/day-04/src/bin/part_one.rs
Normal file
@ -0,0 +1,18 @@
|
||||
use std::{env, io};
|
||||
|
||||
use aoc_2022_04::{parse_inputs, Section};
|
||||
|
||||
fn sections_overlap((Section(l1, l2), Section(r1, r2)): &(Section, Section)) -> bool {
|
||||
(l1 >= r1 && l2 <= r2) || (r1 >= l1 && r2 <= l2)
|
||||
}
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let infile_path = env::args().nth(1).unwrap();
|
||||
let inputs = parse_inputs(infile_path)?;
|
||||
|
||||
let count = inputs.into_iter().filter(sections_overlap).count();
|
||||
|
||||
println!("{count}");
|
||||
|
||||
Ok(())
|
||||
}
|
21
2022/day-04/src/bin/part_two.rs
Normal file
21
2022/day-04/src/bin/part_two.rs
Normal file
@ -0,0 +1,21 @@
|
||||
use std::{env, io};
|
||||
|
||||
use aoc_2022_04::{parse_inputs, Section};
|
||||
|
||||
fn sections_overlap((Section(l1, l2), Section(r1, r2)): &(Section, Section)) -> bool {
|
||||
(r1 >= l1 && r1 <= l2)
|
||||
|| (r2 <= l2 && r2 >= l1)
|
||||
|| (l1 >= r1 && l1 <= r2)
|
||||
|| (l2 <= r2 && l2 >= r1)
|
||||
}
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let infile_path = env::args().nth(1).unwrap();
|
||||
let inputs = parse_inputs(infile_path)?;
|
||||
|
||||
let count = inputs.into_iter().filter(sections_overlap).count();
|
||||
|
||||
println!("{count}");
|
||||
|
||||
Ok(())
|
||||
}
|
37
2022/day-04/src/lib.rs
Normal file
37
2022/day-04/src/lib.rs
Normal file
@ -0,0 +1,37 @@
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{self, BufRead, BufReader},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
// just impl Copy here, it'll be fine TM
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Section(pub u32, pub u32);
|
||||
|
||||
pub fn parse_inputs<P>(path: P) -> io::Result<Vec<(Section, Section)>>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
let file = File::open(path)?;
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
let inputs = reader
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let line = line.unwrap();
|
||||
let parts = line
|
||||
.split(",")
|
||||
.map(|sections| {
|
||||
let section = sections
|
||||
.split("-")
|
||||
.map(|num| num.parse::<u32>().unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
Section(section[0], section[1])
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
(parts[0], parts[1])
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
Ok(inputs)
|
||||
}
|
Loading…
Reference in New Issue
Block a user