Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Oct 20, 2024
1 parent d552aa9 commit 515f877
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 111 deletions.
57 changes: 12 additions & 45 deletions src/problem_2211_count_collisions_on_a_road/iterative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
1 change: 0 additions & 1 deletion src/problem_2211_count_collisions_on_a_road/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod iterative;
pub mod iterative_2;
pub mod tail_recursive;

pub trait Solution {
fn count_collisions(directions: String) -> i32;
Expand Down
65 changes: 0 additions & 65 deletions src/problem_2211_count_collisions_on_a_road/tail_recursive.rs

This file was deleted.

0 comments on commit 515f877

Please sign in to comment.