-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2211: Count Collisions on a Road
- Loading branch information
Showing
4 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
src/problem_2211_count_collisions_on_a_road/iterative_2.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |