From 4f35f764543adc898c4b509c86f0f05a77808337 Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Wed, 21 Aug 2024 22:02:19 +0200 Subject: [PATCH] Wrote program for Day 27 --- README.md | 2 +- .../Day-27_Task-Scheduler/day27/Cargo.toml | 6 ++++ .../Day-27_Task-Scheduler/day27/src/lib.rs | 35 +++++++++++++++++++ .../Day-27_Task-Scheduler/day27/src/main.rs | 33 +++++++++++++++++ .../day27/tests/examples.rs | 25 +++++++++++++ 5 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 Week-04/Day-27_Task-Scheduler/day27/Cargo.toml create mode 100644 Week-04/Day-27_Task-Scheduler/day27/src/lib.rs create mode 100644 Week-04/Day-27_Task-Scheduler/day27/src/main.rs create mode 100644 Week-04/Day-27_Task-Scheduler/day27/tests/examples.rs diff --git a/README.md b/README.md index 3981ebf..e0f899b 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #24 | [The Josephus Problem](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-24_The-Josephus-Problem) | :white_check_mark: | | Day #25 | [Coin Trouble](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-25_Coin-Trouble) | :white_check_mark: | | Day #26 | [Briefcase Lock](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-26_Briefcase-Lock) | :white_check_mark: | -| Day #27 | [Task Scheduler](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-27_Task-Scheduler) | :white_large_square: | +| Day #27 | [Task Scheduler](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-27_Task-Scheduler) | :white_check_mark: | | Day #28 | [Word Search](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-28_Word-Search) | :white_large_square: | | Day #29 | [Traffic Light Checker](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-29_Traffic-Light-Checker) | :white_large_square: | | Day #30 | [The Maximum Value](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-30_The-Maximum-Value) | :white_large_square: | diff --git a/Week-04/Day-27_Task-Scheduler/day27/Cargo.toml b/Week-04/Day-27_Task-Scheduler/day27/Cargo.toml new file mode 100644 index 0000000..71d364d --- /dev/null +++ b/Week-04/Day-27_Task-Scheduler/day27/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day27" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-04/Day-27_Task-Scheduler/day27/src/lib.rs b/Week-04/Day-27_Task-Scheduler/day27/src/lib.rs new file mode 100644 index 0000000..3e3cc3d --- /dev/null +++ b/Week-04/Day-27_Task-Scheduler/day27/src/lib.rs @@ -0,0 +1,35 @@ +use std::collections::HashMap; + +pub fn task_scheduler(tasks: &[char], coldown: u8) -> u16 { + let mut num_tasks = HashMap::new(); + for task in tasks { + *num_tasks.entry(task).or_insert(0) += 1; + } + + let mut num_tasks: Vec<(&&char, &i32)> = num_tasks.iter().collect(); + num_tasks.sort_by(|a, b| b.1.cmp(a.1)); + + let mut result = 0; + let mut pos = 0; + let mut range = 1; + + for _ in 0..tasks.len() { + if num_tasks.get(pos as usize).is_none() { + result += if pos > coldown as u16 { + 0 + } else { + coldown as u16 - pos + 1 + }; + pos = 0; + range += 1; + } + if *num_tasks.get(pos as usize).unwrap().1 < range { + result += coldown as u16 - pos; + pos = 0; + range += 1; + } + result += 1; + pos += 1; + } + result +} diff --git a/Week-04/Day-27_Task-Scheduler/day27/src/main.rs b/Week-04/Day-27_Task-Scheduler/day27/src/main.rs new file mode 100644 index 0000000..ac64a3d --- /dev/null +++ b/Week-04/Day-27_Task-Scheduler/day27/src/main.rs @@ -0,0 +1,33 @@ +use std::io::{self, Write}; + +use day27::task_scheduler; + +fn main() { + let mut buffer = String::new(); + + print!("Insert the task as a unique string: "); + io::stdout().flush().expect("Failed to flush"); + + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read line"); + + let tasks: Vec = buffer.trim().chars().collect(); + + buffer = String::new(); + + print!("Insert the length of the coldown: "); + io::stdout().flush().expect("Failed to flush"); + + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read line"); + + let coldown = buffer + .trim() + .parse() + .expect("Coldown must be a valid unsigned integer"); + + let result = task_scheduler(&tasks, coldown); + println!("The least number of units of times that the CPU will take to finish all the given tasks is {}", result); +} diff --git a/Week-04/Day-27_Task-Scheduler/day27/tests/examples.rs b/Week-04/Day-27_Task-Scheduler/day27/tests/examples.rs new file mode 100644 index 0000000..6698f68 --- /dev/null +++ b/Week-04/Day-27_Task-Scheduler/day27/tests/examples.rs @@ -0,0 +1,25 @@ +#[cfg(test)] +mod examples { + use day27::task_scheduler; + + #[test] + fn example1() { + assert_eq!(task_scheduler(&['A', 'A', 'A', 'B', 'B', 'B'], 2), 8); + } + + #[test] + fn example2() { + assert_eq!(task_scheduler(&['A', 'A', 'A', 'B', 'B', 'B'], 0), 6); + } + + #[test] + fn example3() { + assert_eq!( + task_scheduler( + &['A', 'A', 'A', 'A', 'A', 'A', 'B', 'C', 'D', 'E', 'F', 'G'], + 2 + ), + 16 + ); + } +}