diff --git a/README.md b/README.md index 20bc83d..6654d4e 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | 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_check_mark: | | Day #28 | [Word Search](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-28_Word-Search) | :white_check_mark: | -| 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 #29 | [Traffic Light Checker](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-29_Traffic-Light-Checker) | :white_check_mark: | | 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: | | Day #31 | [The Time In Words](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-31_The-Time-In-Words) | :white_large_square: | | Day #32 | [Climbing The Leaderboard](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-32_Climbing-The-Leaderboard) | :white_large_square: | diff --git a/Week-05/Day-29_Traffic-Light-Checker/day29/Cargo.toml b/Week-05/Day-29_Traffic-Light-Checker/day29/Cargo.toml new file mode 100644 index 0000000..479a025 --- /dev/null +++ b/Week-05/Day-29_Traffic-Light-Checker/day29/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day29" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-05/Day-29_Traffic-Light-Checker/day29/src/lib.rs b/Week-05/Day-29_Traffic-Light-Checker/day29/src/lib.rs new file mode 100644 index 0000000..7c785a0 --- /dev/null +++ b/Week-05/Day-29_Traffic-Light-Checker/day29/src/lib.rs @@ -0,0 +1,58 @@ +pub fn tl_checker(lights: &[char]) -> TLR { + if lights.len() < 4 || lights.len() > 15 { + return TLR::Error; + } + + let mut previous = &'X'; + + for light in lights { + if !matches!(light, 'R' | 'Y' | 'G' | 'P' | 'C') { + return TLR::Error; + } + + match previous { + 'R' => { + if !matches!(light, 'G' | 'P' | 'C') { + return TLR::Reject; + } + } + 'Y' => { + if light != &'R' { + return TLR::Reject; + } + } + 'G' => { + if light != &'Y' { + return TLR::Reject; + } + } + 'P' => { + if light != &'R' { + return TLR::Reject; + } + } + 'C' => { + if light != &'R' { + return TLR::Reject; + } + } + 'X' => { + if light != &'R' { + return TLR::Reject; + } + } + _ => return TLR::Error, + } + + previous = light; + } + + TLR::Accept +} + +#[derive(Debug, PartialEq)] +pub enum TLR { + Accept, + Reject, + Error, +} diff --git a/Week-05/Day-29_Traffic-Light-Checker/day29/src/main.rs b/Week-05/Day-29_Traffic-Light-Checker/day29/src/main.rs new file mode 100644 index 0000000..2fccec6 --- /dev/null +++ b/Week-05/Day-29_Traffic-Light-Checker/day29/src/main.rs @@ -0,0 +1,23 @@ +use std::io::{self, Write}; + +use day29::tl_checker; + +fn main() { + let mut buffer = String::new(); + + print!("Enter the traffic light sequence: "); + io::stdout().flush().expect("Failed to flush stdout"); + + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read the sequence"); + + let sequence: Vec = buffer.chars().filter(|c| !c.is_whitespace()).collect(); + + let result = tl_checker(&sequence); + match result { + day29::TLR::Accept => println!("The sequence is accepted"), + day29::TLR::Reject => println!("The sequence is rejected"), + day29::TLR::Error => println!("The sequence is invalid"), + } +} diff --git a/Week-05/Day-29_Traffic-Light-Checker/day29/tests/examples.rs b/Week-05/Day-29_Traffic-Light-Checker/day29/tests/examples.rs new file mode 100644 index 0000000..52f1998 --- /dev/null +++ b/Week-05/Day-29_Traffic-Light-Checker/day29/tests/examples.rs @@ -0,0 +1,42 @@ +#[cfg(test)] +mod examples { + use day29::{tl_checker, TLR}; + + #[test] + fn example1() { + assert_eq!( + tl_checker(&['R', 'G', 'Y', 'R', 'C', 'R', 'G', 'Y', 'R']), + TLR::Accept + ); + } + + #[test] + fn example2() { + assert_eq!(tl_checker(&['G', 'Y', 'R', 'G', 'Y', 'R']), TLR::Reject); + } + + #[test] + fn example3() { + assert_eq!(tl_checker(&['R', 'Y', 'G', 'P']), TLR::Reject); + } + + #[test] + fn example4() { + assert_eq!(tl_checker(&['R', 'G', 'Y']), TLR::Error); + } + + #[test] + fn example5() { + assert_eq!(tl_checker(&['X', '8', 'S']), TLR::Error); + } + + #[test] + fn example6() { + assert_eq!( + tl_checker(&[ + 'R', 'G', 'Y', 'R', 'C', 'R', 'P', 'R', 'G', 'Y', 'R', 'G', 'Y', 'R', 'G', 'Y', 'R' + ]), + TLR::Error + ); + } +}