From dc46978ac442d5fcefdb77c98c9f99c8eb96256a Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Wed, 4 Sep 2024 10:26:34 +0200 Subject: [PATCH] Wrote program for Day 41 --- README.md | 2 +- .../Day-41_Sales-By-Match/day41/Cargo.toml | 6 ++++ .../Day-41_Sales-By-Match/day41/src/lib.rs | 18 +++++++++++ .../Day-41_Sales-By-Match/day41/src/main.rs | 30 +++++++++++++++++++ .../day41/tests/example.rs | 9 ++++++ 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 Week-06/Day-41_Sales-By-Match/day41/Cargo.toml create mode 100644 Week-06/Day-41_Sales-By-Match/day41/src/lib.rs create mode 100644 Week-06/Day-41_Sales-By-Match/day41/src/main.rs create mode 100644 Week-06/Day-41_Sales-By-Match/day41/tests/example.rs diff --git a/README.md b/README.md index e9682ef..b22299b 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #38 | [Electronics Shop](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-38_Electronics-Shop) | :white_check_mark: | | Day #39 | [Halloween Sale](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-39_Halloween-Sale) | :white_check_mark: | | Day #40 | [Larrys Array](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-40_Larrys-Array) | :white_check_mark: | -| Day #41 | [Sales By Match](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-41_Sales-By-Match) | :white_large_square: | +| Day #41 | [Sales By Match](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-41_Sales-By-Match) | :white_check_mark: | | Day #42 | [Drawing Book](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-42_Drawing-Book) | :white_large_square: | | Day #43 | [Area Of A Triangle](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-43_Area-Of-A-Triangle) | :white_large_square: | | Day #44 | [Maximum Edge Of A Triangle](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-44_Maximum-Edge-Of-A-Triangle) | :white_large_square: | diff --git a/Week-06/Day-41_Sales-By-Match/day41/Cargo.toml b/Week-06/Day-41_Sales-By-Match/day41/Cargo.toml new file mode 100644 index 0000000..6b0b025 --- /dev/null +++ b/Week-06/Day-41_Sales-By-Match/day41/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day41" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-06/Day-41_Sales-By-Match/day41/src/lib.rs b/Week-06/Day-41_Sales-By-Match/day41/src/lib.rs new file mode 100644 index 0000000..1b6b62f --- /dev/null +++ b/Week-06/Day-41_Sales-By-Match/day41/src/lib.rs @@ -0,0 +1,18 @@ +use std::collections::HashMap; + +pub fn socks_match(socks: &[usize]) -> usize { + let mut count: HashMap<&usize, usize> = HashMap::new(); + let mut result = 0; + + for sock in socks { + count.entry(sock).and_modify(|x| *x += 1).or_default(); + if count[sock] == 2 { + result += 1; + if let Some(x) = count.get_mut(&sock) { + *x = 0; + } + } + } + + result +} diff --git a/Week-06/Day-41_Sales-By-Match/day41/src/main.rs b/Week-06/Day-41_Sales-By-Match/day41/src/main.rs new file mode 100644 index 0000000..6230cd0 --- /dev/null +++ b/Week-06/Day-41_Sales-By-Match/day41/src/main.rs @@ -0,0 +1,30 @@ +use std::{ + io::{self, Write}, + process::exit, +}; + +use day41::socks_match; + +fn main() { + print!("Please enter the colors of the socks as a list of numbers, separated by spaces: "); + io::stdout().flush().expect("Failed to flush stdout"); + + let mut buffer = String::new(); + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read line"); + + let socks: Vec = buffer + .split_whitespace() + .map(|x| match x.parse() { + Ok(x) => x, + Err(e) => { + eprintln!("{e}"); + exit(1); + } + }) + .collect(); + + let count = socks_match(&socks); + println!("The number of pairs is {count}."); +} diff --git a/Week-06/Day-41_Sales-By-Match/day41/tests/example.rs b/Week-06/Day-41_Sales-By-Match/day41/tests/example.rs new file mode 100644 index 0000000..754d9b6 --- /dev/null +++ b/Week-06/Day-41_Sales-By-Match/day41/tests/example.rs @@ -0,0 +1,9 @@ +#[cfg(test)] +mod example { + use day41::socks_match; + + #[test] + fn test_example() { + assert_eq!(socks_match(&[1, 2, 1, 2, 1, 3, 2]), 2); + } +}