-
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.
- Loading branch information
Showing
3 changed files
with
59 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,40 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
#[allow(clippy::naive_bytecount)] // Not supported by LeetCode. | ||
pub fn count_points(rings: String) -> i32 { | ||
let mut iter_2 = rings.bytes(); | ||
|
||
iter_2.next(); | ||
|
||
let mut states = [0_u8; 10]; | ||
|
||
for (color, pole) in rings.bytes().zip(iter_2).step_by(2) { | ||
states[usize::from(pole) - usize::from(b'0')] |= match color { | ||
b'B' => 1, | ||
b'G' => 2, | ||
_ => 4, | ||
}; | ||
} | ||
|
||
states.iter().filter(|&&x| x == 7).count() as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn count_points(rings: String) -> i32 { | ||
Self::count_points(rings) | ||
} | ||
} | ||
|
||
#[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,18 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn count_points(rings: String) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [("B0B6G0R6R0R6G9", 1), ("B0R0G0R9R0B0G0", 1), ("G4", 0)]; | ||
|
||
for (rings, expected) in test_cases { | ||
assert_eq!(S::count_points(rings.to_string()), expected); | ||
} | ||
} | ||
} |