feat: day4
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
603f05ef5e
commit
7da14f16a4
@ -18,3 +18,8 @@ day-3:
|
|||||||
stage: build
|
stage: build
|
||||||
script:
|
script:
|
||||||
- cd day3; cargo run --release ./input
|
- cd day3; cargo run --release ./input
|
||||||
|
|
||||||
|
day-4:
|
||||||
|
stage: build
|
||||||
|
script:
|
||||||
|
- cd day4; cargo run --release ./input
|
||||||
|
8
day4/Cargo.toml
Normal file
8
day4/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "day3"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
1000
day4/input
Normal file
1000
day4/input
Normal file
File diff suppressed because it is too large
Load Diff
33
day4/src/main.rs
Normal file
33
day4/src/main.rs
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
use std::{fs, env, vec};
|
||||||
|
|
||||||
|
|
||||||
|
fn read_input(path: &str) -> String {
|
||||||
|
return fs::read_to_string(path).expect("Cannot read file.");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_input(s: &str) -> Vec<((u32, u32), (u32, u32))> {
|
||||||
|
return s.lines().into_iter().fold(vec![], |mut acc, x| {
|
||||||
|
let numbers: Vec<u32> = x.split(&['-', ',']).map(|s| s.parse::<u32>().unwrap()).collect();
|
||||||
|
acc.push(((numbers[0], numbers[1]), (numbers[2], numbers[3])));
|
||||||
|
return acc;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fully_contains(t1: &(u32, u32), t2: &(u32, u32)) -> bool {
|
||||||
|
return (t1.0 >= t2.0 && t1.1 <= t2.1) || (t1.0 <= t2.0 && t1.1 >= t2.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn partially_contains(t1: &(u32, u32), t2: &(u32, u32)) -> bool {
|
||||||
|
return (t2.0 <= t1.1 && t2.0 >= t1.0) || (t2.1 <= t1.1 && t2.1 >= t1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let args: Vec<String> = env::args().collect();
|
||||||
|
for arg in args.iter().skip(1) {
|
||||||
|
println!("[{}]", &arg);
|
||||||
|
let vec_in = parse_input(&read_input(&arg));
|
||||||
|
println!("\t[Part 1] => Answer is '{}'.", vec_in.iter().filter(|t| fully_contains(&t.0, &t.1)).count());
|
||||||
|
println!("\t[Part 2] => Answer is '{}'.", vec_in.iter().filter(|t| fully_contains(&t.0, &t.1) || partially_contains(&t.0, &t.1)).count());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user