Skip to content

Commit

Permalink
Add problem 2211: Count Collisions on a Road
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Oct 20, 2024
1 parent a939c6a commit 1d605af
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,7 @@ pub mod problem_2207_maximize_number_of_subsequences_in_a_string;
pub mod problem_2208_minimum_operations_to_halve_array_sum;
pub mod problem_2209_minimum_white_tiles_after_covering_with_carpets;
pub mod problem_2210_count_hills_and_valleys_in_an_array;
pub mod problem_2211_count_collisions_on_a_road;
pub mod problem_2215_find_the_difference_of_two_arrays;
pub mod problem_2216_minimum_deletions_to_make_array_beautiful;
pub mod problem_2218_maximum_value_of_k_coins_from_piles;
Expand Down
79 changes: 79 additions & 0 deletions src/problem_2211_count_collisions_on_a_road/iterative.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

enum State {
AllLeft,
Stop,
Right(u32),
}

impl Solution {
pub fn count_collisions(directions: String) -> i32 {
let mut result = 0;
let mut state = State::AllLeft;
let mut iter = directions.bytes();

loop {
match state {
State::AllLeft => {
if let Some(c) = iter.next() {
match c {
b'L' => {}
b'R' => state = State::Right(1),
_ => state = State::Stop,
}
} else {
break;
}
}
State::Stop => {
if let Some(c) = iter.next() {
match c {
b'L' => result += 1,
b'R' => state = State::Right(1),
_ => {}
}
} else {
break;
}
}
State::Right(count) => {
if let Some(c) = iter.next() {
match c {
b'L' => {
result += count + 1;
state = State::Stop;
}
b'R' => state = State::Right(count + 1),
_ => {
result += count;
state = State::Stop;
}
}
} else {
break;
}
}
}
}

result as _
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn count_collisions(directions: String) -> i32 {
Self::count_collisions(directions)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
32 changes: 32 additions & 0 deletions src/problem_2211_count_collisions_on_a_road/iterative_2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
pub fn count_collisions(directions: String) -> i32 {
let directions = directions.into_bytes();
let left = directions.iter().take_while(|&&c| c == b'L').count();
let right = directions.iter().rev().take_while(|&&c| c == b'R').count();

directions[left..directions.len() - right]
.iter()
.filter(|&&c| c != b'S')
.count() as _
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn count_collisions(directions: String) -> i32 {
Self::count_collisions(directions)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
19 changes: 19 additions & 0 deletions src/problem_2211_count_collisions_on_a_road/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pub mod iterative;
pub mod iterative_2;

pub trait Solution {
fn count_collisions(directions: String) -> i32;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [("RLRSLL", 5), ("LLRR", 0), ("LLSRSSRSSLLSLLLRSLSRL", 11), ("L", 0)];

for (directions, expected) in test_cases {
assert_eq!(S::count_collisions(directions.to_string()), expected);
}
}
}

0 comments on commit 1d605af

Please sign in to comment.