-
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 2086: Minimum Number of Food Buckets to Feed the Hamsters
- Loading branch information
Showing
5 changed files
with
273 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
28 changes: 28 additions & 0 deletions
28
src/problem_2086_minimum_number_of_food_buckets_to_feed_the_hamsters/mod.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,28 @@ | ||
pub mod state_machine; | ||
pub mod state_machine_2; | ||
pub mod state_machine_3; | ||
|
||
pub trait Solution { | ||
fn minimum_buckets(hamsters: String) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
("H..H", 2), | ||
(".H.H.", 1), | ||
(".HHH.", -1), | ||
("..", 0), | ||
("H", -1), | ||
(".HH.H", 2), | ||
("...HH...H.", 3), | ||
]; | ||
|
||
for (hamsters, expected) in test_cases { | ||
assert_eq!(S::minimum_buckets(hamsters.to_string()), expected); | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
src/problem_2086_minimum_number_of_food_buckets_to_feed_the_hamsters/state_machine.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,88 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
enum State { | ||
Start, | ||
Empty, | ||
Hamster, | ||
EmptyHamster, | ||
Food, | ||
} | ||
|
||
impl Solution { | ||
pub fn minimum_buckets(hamsters: String) -> i32 { | ||
let mut result = 0; | ||
let mut state = State::Start; | ||
|
||
for c in hamsters.into_bytes() { | ||
let is_empty = c == b'.'; | ||
|
||
state = match state { | ||
State::Start => { | ||
if is_empty { | ||
State::Empty | ||
} else { | ||
State::Hamster | ||
} | ||
} | ||
State::Empty => { | ||
if is_empty { | ||
State::Empty | ||
} else { | ||
State::EmptyHamster | ||
} | ||
} | ||
State::Hamster => { | ||
if is_empty { | ||
result += 1; | ||
|
||
State::Food | ||
} else { | ||
return -1; | ||
} | ||
} | ||
State::EmptyHamster => { | ||
result += 1; | ||
|
||
if is_empty { | ||
State::Food | ||
} else { | ||
State::Hamster | ||
} | ||
} | ||
State::Food => { | ||
if is_empty { | ||
State::Empty | ||
} else { | ||
State::Start | ||
} | ||
} | ||
}; | ||
} | ||
|
||
result += match state { | ||
State::Start | State::Empty | State::Food => 0, | ||
State::Hamster => return -1, | ||
State::EmptyHamster => 1, | ||
}; | ||
|
||
result | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn minimum_buckets(hamsters: String) -> i32 { | ||
Self::minimum_buckets(hamsters) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/problem_2086_minimum_number_of_food_buckets_to_feed_the_hamsters/state_machine_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,87 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::str::Bytes; | ||
|
||
struct Context<'a> { | ||
iter: Bytes<'a>, | ||
result: i32, | ||
} | ||
|
||
impl Context<'_> { | ||
#[inline(never)] | ||
fn start(&mut self) { | ||
match self.iter.next() { | ||
None => {} | ||
Some(b'.') => self.empty(), | ||
Some(_) => self.hamster(), | ||
} | ||
} | ||
|
||
fn empty(&mut self) { | ||
match self.iter.next() { | ||
None => {} | ||
Some(b'.') => self.empty(), | ||
Some(_) => self.empty_hamster(), | ||
} | ||
} | ||
|
||
fn hamster(&mut self) { | ||
match self.iter.next() { | ||
Some(b'.') => { | ||
self.result += 1; | ||
|
||
self.food(); | ||
} | ||
_ => self.result = -1, | ||
} | ||
} | ||
|
||
fn empty_hamster(&mut self) { | ||
self.result += 1; | ||
|
||
match self.iter.next() { | ||
None => {} | ||
Some(b'.') => self.food(), | ||
Some(_) => self.hamster(), | ||
} | ||
} | ||
|
||
fn food(&mut self) { | ||
match self.iter.next() { | ||
None => {} | ||
Some(b'.') => self.empty(), | ||
Some(_) => self.start(), | ||
} | ||
} | ||
} | ||
|
||
impl Solution { | ||
pub fn minimum_buckets(hamsters: String) -> i32 { | ||
let mut context = Context { | ||
iter: hamsters.bytes(), | ||
result: 0, | ||
}; | ||
|
||
context.start(); | ||
|
||
context.result | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn minimum_buckets(hamsters: String) -> i32 { | ||
Self::minimum_buckets(hamsters) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/problem_2086_minimum_number_of_food_buckets_to_feed_the_hamsters/state_machine_3.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,69 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::str::Bytes; | ||
|
||
impl Solution { | ||
#[inline(never)] | ||
fn start(mut iter: Bytes, result: i32) -> i32 { | ||
match iter.next() { | ||
None => result, | ||
Some(b'.') => Self::empty(iter, result), | ||
Some(_) => Self::hamster(iter, result), | ||
} | ||
} | ||
|
||
fn empty(mut iter: Bytes, result: i32) -> i32 { | ||
match iter.next() { | ||
None => result, | ||
Some(b'.') => Self::empty(iter, result), | ||
Some(_) => Self::empty_hamster(iter, result), | ||
} | ||
} | ||
|
||
fn hamster(mut iter: Bytes, result: i32) -> i32 { | ||
match iter.next() { | ||
Some(b'.') => Self::food(iter, result + 1), | ||
_ => -1, | ||
} | ||
} | ||
|
||
fn empty_hamster(mut iter: Bytes, mut result: i32) -> i32 { | ||
result += 1; | ||
|
||
match iter.next() { | ||
None => result, | ||
Some(b'.') => Self::food(iter, result), | ||
Some(_) => Self::hamster(iter, result), | ||
} | ||
} | ||
|
||
fn food(mut iter: Bytes, result: i32) -> i32 { | ||
match iter.next() { | ||
None => result, | ||
Some(b'.') => Self::empty(iter, result), | ||
Some(_) => Self::start(iter, result), | ||
} | ||
} | ||
|
||
pub fn minimum_buckets(hamsters: String) -> i32 { | ||
Self::start(hamsters.bytes(), 0) | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn minimum_buckets(hamsters: String) -> i32 { | ||
Self::minimum_buckets(hamsters) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |