diff options
| author | a14m <[email protected]> | 2021-04-06 20:35:13 +0200 |
|---|---|---|
| committer | a14m <[email protected]> | 2021-04-06 20:35:13 +0200 |
| commit | a604a6dbf0779c001d145297eb1fb1a1164882f9 (patch) | |
| tree | beaef4aff7e1551b231a7cc3452e341bacc21a16 /CLI/rust/src/game_of_life/generators/input.rs | |
| parent | 4b71f71b0b633279e29f8b34dfd0da1426f34cc8 (diff) | |
Refactor populate to work with strings and generate vec
Diffstat (limited to 'CLI/rust/src/game_of_life/generators/input.rs')
| -rw-r--r-- | CLI/rust/src/game_of_life/generators/input.rs | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/CLI/rust/src/game_of_life/generators/input.rs b/CLI/rust/src/game_of_life/generators/input.rs index 4f205cb..9584cee 100644 --- a/CLI/rust/src/game_of_life/generators/input.rs +++ b/CLI/rust/src/game_of_life/generators/input.rs @@ -78,16 +78,7 @@ fn generate_file_data(opts: Opts) -> Result<Universe, Error> { )) } }; - let data: Vec<Vec<bool>> = file - .split("\n") - .map(|s| { - s.to_string() - .chars() - .map(|c| c.is_ascii_alphanumeric()) - .collect() - }) - .collect(); - Ok(populate(opts, data)) + Ok(populate(opts, file)) } #[cfg(test)] @@ -148,8 +139,18 @@ fn generate_url_data(_opts: Opts) -> Result<Universe, Error> { /// Return a universe struct with populated data from the data param /// /// The data param is reshaped to fit with the opts width/height (extended or shrinked) -fn populate(opts: Opts, data: Vec<Vec<bool>>) -> Universe { +fn populate(opts: Opts, raw_data: String) -> Universe { let (width, height) = (opts.width as usize, opts.height as usize); + let data: Vec<Vec<bool>> = raw_data + .split("\n") + .map(|s| { + s.to_string() + .chars() + .map(|c| c.is_ascii_alphanumeric()) + .collect() + }) + .collect(); + let mut current = vec![vec![false; width]; height]; for i in 0..height { @@ -188,7 +189,7 @@ mod populate { #[test] fn expand_data() { - let data = vec![vec![true, false, true]; 3]; + let data = String::from("x.x\nO.0\nO.X"); let opts = opts(5, 5); let universe = populate(opts.clone(), data); @@ -210,7 +211,7 @@ mod populate { #[test] fn shrink_data() { - let data = vec![vec![true, false, true]; 3]; + let data = String::from("x.x\nO.0\nO.X"); let opts = opts(2, 2); let universe = populate(opts.clone(), data); |
