From aee8ae9a4fba1fb3389c36c682be77f35da98170 Mon Sep 17 00:00:00 2001 From: Pcornat Date: Thu, 8 Dec 2022 22:07:36 +0100 Subject: [PATCH 1/3] getting rid of useless file --- .gitignore | 16 +++++++++++++++- Cargo.lock | 7 ------- 2 files changed, 15 insertions(+), 8 deletions(-) delete mode 100644 Cargo.lock diff --git a/.gitignore b/.gitignore index 2a0038a..d9f5ff0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,16 @@ /target -.idea \ No newline at end of file +.idea +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb diff --git a/Cargo.lock b/Cargo.lock deleted file mode 100644 index b269bcf..0000000 --- a/Cargo.lock +++ /dev/null @@ -1,7 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "advent_of_code" -version = "0.1.0" From 83c16d7fb7a282023ec4472dd29e21fa8337d6ce Mon Sep 17 00:00:00 2001 From: Pcornat Date: Thu, 8 Dec 2022 22:07:50 +0100 Subject: [PATCH 2/3] Personnal profile (release) --- Cargo.toml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 335d3fb..91123ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,4 +5,9 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[profile.release-perso] +inherits = "release" +incremental = true +rpath = true + [dependencies] From befbc009cc8d3ebfd8e3ff8899a22dc1414a9069 Mon Sep 17 00:00:00 2001 From: Pcornat Date: Thu, 8 Dec 2022 22:08:01 +0100 Subject: [PATCH 3/3] 4th problem part 1 done ! --- src/main.rs | 7 ++++++- src/prob4/mod.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) 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() +}