diff --git a/src/main.rs b/src/main.rs index c74fc3f..97a0e1a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use advent_of_code::{prob1, prob2, prob3}; +use advent_of_code::{prob1, prob2, prob3, prob4}; fn main() { { @@ -23,4 +23,9 @@ fn main() { let max_part2 = prob3::solve_part2(file_path); println!("Solution for problem 3 part 2: {max_part2}"); } + { + let file_path = "prob4_part1.txt"; + let max_part1 = prob4::solve_part1(file_path); + println!("Solution for problem 4 part 1: {max_part1}"); + } } diff --git a/src/prob4/mod.rs b/src/prob4/mod.rs index e69de29..02c29dc 100644 --- a/src/prob4/mod.rs +++ b/src/prob4/mod.rs @@ -0,0 +1,40 @@ +fn mutual_inclusion(first_pair: &[u32], second_pair: &[u32]) -> bool { + (first_pair[0] <= second_pair[0] && first_pair[1] >= second_pair[1]) + || (second_pair[0] <= first_pair[0] && second_pair[1] >= first_pair[1]) +} + +pub fn solve_part1(file_path: &str) -> i32 { + let result = std::fs::read_to_string(file_path) + .expect("Please provide a text file as an argument.") + .lines() + .map(|line| { + let tmp = line.split(',').collect::>(); + let firs_pair = tmp + .first() + .expect("First character error") + .split('-') + .map(|first_split| { + first_split + .parse::() + .expect("Parsing number in first pair failed") + }) + .collect::>(); + let second_pair = tmp + .last() + .expect("First character error") + .split('-') + .map(|first_split| { + first_split + .parse::() + .expect("Parsing number in first pair failed") + }) + .collect::>(); + if mutual_inclusion(&firs_pair, &second_pair) { + 1 + } else { + 0 + } + }) + .collect::>(); + result.iter().sum() +}