-
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 2094: Finding 3-Digit Even Numbers
- Loading branch information
Showing
4 changed files
with
157 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
62 changes: 62 additions & 0 deletions
62
src/problem_2094_finding_3_digit_even_numbers/brute_force.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,62 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::cell::Cell; | ||
|
||
impl Solution { | ||
pub fn find_even_numbers(digits: Vec<i32>) -> Vec<i32> { | ||
#[allow(clippy::declare_interior_mutable_const)] // TODO: Use inline | ||
const ZERO_CELL: Cell<u8> = Cell::new(0); | ||
|
||
let mut counts = [ZERO_CELL; 10]; | ||
|
||
for digit in digits { | ||
*counts[digit as u32 as usize].get_mut() += 1; | ||
} | ||
|
||
let mut result = Vec::new(); | ||
|
||
for candidate in (100_u32..1000).step_by(2) { | ||
let first_count = &counts[(candidate / 100) as usize]; | ||
|
||
if first_count.get() != 0 { | ||
first_count.set(first_count.get() - 1); | ||
|
||
let second_count = &counts[((candidate / 10) % 10) as usize]; | ||
|
||
if second_count.get() != 0 { | ||
second_count.set(second_count.get() - 1); | ||
|
||
let third_count = &counts[(candidate % 10) as usize]; | ||
|
||
if third_count.get() != 0 { | ||
result.push(candidate as _); | ||
} | ||
|
||
second_count.set(second_count.get() + 1); | ||
} | ||
|
||
first_count.set(first_count.get() + 1); | ||
} | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn find_even_numbers(digits: Vec<i32>) -> Vec<i32> { | ||
Self::find_even_numbers(digits) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/problem_2094_finding_3_digit_even_numbers/brute_force_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,68 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::cell::Cell; | ||
|
||
impl Solution { | ||
pub fn find_even_numbers(digits: Vec<i32>) -> Vec<i32> { | ||
#[allow(clippy::declare_interior_mutable_const)] // TODO: Use inline | ||
const ZERO_CELL: Cell<u8> = Cell::new(0); | ||
|
||
let mut counts = [ZERO_CELL; 10]; | ||
|
||
for digit in digits { | ||
*counts[digit as u32 as usize].get_mut() += 1; | ||
} | ||
|
||
let mut result = Vec::new(); | ||
|
||
for first in 1..10 { | ||
let first_count = &counts[first]; | ||
|
||
if first_count.get() != 0 { | ||
let value = first as i32 * 100; | ||
|
||
first_count.set(first_count.get() - 1); | ||
|
||
for second in 0..10 { | ||
let second_count = &counts[second]; | ||
|
||
if second_count.get() != 0 { | ||
let value = value + second as i32 * 10; | ||
|
||
second_count.set(second_count.get() - 1); | ||
|
||
for third in (0..10).step_by(2) { | ||
if counts[third].get() != 0 { | ||
result.push(value + third as i32); | ||
} | ||
} | ||
|
||
second_count.set(second_count.get() + 1); | ||
} | ||
} | ||
|
||
first_count.set(first_count.get() + 1); | ||
} | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn find_even_numbers(digits: Vec<i32>) -> Vec<i32> { | ||
Self::find_even_numbers(digits) | ||
} | ||
} | ||
|
||
#[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,26 @@ | ||
pub mod brute_force; | ||
pub mod brute_force_2; | ||
|
||
pub trait Solution { | ||
fn find_even_numbers(digits: Vec<i32>) -> Vec<i32>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
( | ||
&[2, 1, 3, 0] as &[_], | ||
&[102, 120, 130, 132, 210, 230, 302, 310, 312, 320] as &[_], | ||
), | ||
(&[2, 2, 8, 8, 2], &[222, 228, 282, 288, 822, 828, 882]), | ||
(&[3, 7, 5], &[]), | ||
]; | ||
|
||
for (digits, expected) in test_cases { | ||
assert_eq!(S::find_even_numbers(digits.to_vec()), expected); | ||
} | ||
} | ||
} |