Skip to content

Commit

Permalink
Add problem 2103: Rings and Rods
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Jan 31, 2024
1 parent a8d792a commit d393fe6
Show file tree
Hide file tree
Showing 3 changed files with 59 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 @@ -1434,6 +1434,7 @@ pub mod problem_2090_k_radius_subarray_averages;
pub mod problem_2091_removing_minimum_and_maximum_from_array;
pub mod problem_2095_delete_the_middle_node_of_a_linked_list;
pub mod problem_2099_find_subsequence_of_length_k_with_the_largest_sum;
pub mod problem_2103_rings_and_rods;

#[cfg(test)]
mod test_utilities;
40 changes: 40 additions & 0 deletions src/problem_2103_rings_and_rods/iterative.rs
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>();
}
}
18 changes: 18 additions & 0 deletions src/problem_2103_rings_and_rods/mod.rs
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);
}
}
}

0 comments on commit d393fe6

Please sign in to comment.