From 05e97b0391094a583e0d9fd5141d358cff4b18c3 Mon Sep 17 00:00:00 2001 From: Jim Infield Date: Fri, 23 Jul 2021 13:58:48 -0500 Subject: [PATCH] initial commit --- .gitignore | 1 + Cargo.lock | 5 +++++ Cargo.toml | 8 ++++++++ src/main.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..86f1367 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "deadfish" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..0d2a2ab --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "deadfish" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..8195e15 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,43 @@ +/**** + * + * Deadfish has 4 commands, each 1 character long: + * + * i increments the value (initially 0) + * d decrements the value + * s squares the value + * o outputs the value into the return array + * Invalid characters should be ignored. + * + */ + +fn main() { + println!("\n{:?}\n", parse("ioxiosydoioz")); +} + +fn parse(code: &str) -> Vec { + let mut n: i32 = 0; + let mut v = vec![]; + + for c in code.chars() { + match c { + 'i' => n += 1, + 'd' => n -= 1, + 's' => n *= n, + 'o' => v.push(n), + _ => () + } + } + v +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn sample_tests() { + assert_eq!(parse("iiisdoso"), + vec![8, 64]); + assert_eq!(parse("iiisdosodddddiso"), + vec![8, 64, 3600]); + } +}