diff --git a/src/problem_2211_count_collisions_on_a_road/iterative.rs b/src/problem_2211_count_collisions_on_a_road/iterative.rs index fb4baac4..111b35dd 100644 --- a/src/problem_2211_count_collisions_on_a_road/iterative.rs +++ b/src/problem_2211_count_collisions_on_a_road/iterative.rs @@ -2,59 +2,26 @@ 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; - } - } + for c in iter.by_ref() { + if c != b'L' { + let mut count = u32::from(c == b'R'); + + for c in iter { + if c == b'R' { + count += 1; } else { - break; + result += count + u32::from(c == b'L'); + + count = 0; } } + + break; } } diff --git a/src/problem_2211_count_collisions_on_a_road/mod.rs b/src/problem_2211_count_collisions_on_a_road/mod.rs index 1bf8bd23..25524fd5 100644 --- a/src/problem_2211_count_collisions_on_a_road/mod.rs +++ b/src/problem_2211_count_collisions_on_a_road/mod.rs @@ -1,6 +1,5 @@ pub mod iterative; pub mod iterative_2; -pub mod tail_recursive; pub trait Solution { fn count_collisions(directions: String) -> i32; diff --git a/src/problem_2211_count_collisions_on_a_road/tail_recursive.rs b/src/problem_2211_count_collisions_on_a_road/tail_recursive.rs deleted file mode 100644 index 706b760f..00000000 --- a/src/problem_2211_count_collisions_on_a_road/tail_recursive.rs +++ /dev/null @@ -1,65 +0,0 @@ -pub struct Solution; - -// ------------------------------------------------------ snip ------------------------------------------------------ // - -use std::str::Bytes; - -impl Solution { - fn all_left(mut iter: Bytes, result: u32) -> u32 { - for c in iter.by_ref() { - match c { - b'L' => {} - b'R' => return Self::right(iter, result, 1), - _ => return Self::stop(iter, result), - } - } - - result - } - - #[inline(never)] - fn right(mut iter: Bytes, result: u32, mut count: u32) -> u32 { - for c in iter.by_ref() { - if c == b'R' { - count += 1; - } else { - return Self::stop(iter, result + count + u32::from(c == b'L')); - } - } - - result - } - - #[inline(never)] - fn stop(mut iter: Bytes, mut result: u32) -> u32 { - for c in iter.by_ref() { - if c == b'R' { - return Self::right(iter, result, 1); - } - - result += u32::from(c == b'L'); - } - - result - } - - pub fn count_collisions(directions: String) -> i32 { - Self::all_left(directions.bytes(), 0) 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::(); - } -}