commit 5a9634eb5bfb0367ca02d25abbb889658d8562d3 Author: Joe Ardent Date: Tue Oct 25 14:34:11 2022 -0700 ugh, finally works diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..901b6e6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*/target/ diff --git a/happy_number/Cargo.lock b/happy_number/Cargo.lock new file mode 100644 index 0000000..89a7944 --- /dev/null +++ b/happy_number/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "happy_number" +version = "0.1.0" diff --git a/happy_number/Cargo.toml b/happy_number/Cargo.toml new file mode 100644 index 0000000..29f594e --- /dev/null +++ b/happy_number/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "happy_number" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/happy_number/src/main.rs b/happy_number/src/main.rs new file mode 100644 index 0000000..51e6c84 --- /dev/null +++ b/happy_number/src/main.rs @@ -0,0 +1,23 @@ +struct Solution {} + +impl Solution { + pub fn is_happy(n: i32) -> bool { + let mut s = std::collections::HashSet::new(); + let mut n = n; + while s.insert(n) { + n = Self::sum_squares(n); + } + n == 1 + } + + fn sum_squares(n: i32) -> i32 { + n.to_string() + .chars() + .map(|c| c.to_digit(10).unwrap_or(0).pow(2)) + .sum::() as i32 + } +} + +fn main() { + println!("{}", Solution::is_happy(19)); +} diff --git a/spiral_matrix/Cargo.toml b/spiral_matrix/Cargo.toml new file mode 100644 index 0000000..4143eb0 --- /dev/null +++ b/spiral_matrix/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "spiral_matrix" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/spiral_matrix/src/main.rs b/spiral_matrix/src/main.rs new file mode 100644 index 0000000..12dae67 --- /dev/null +++ b/spiral_matrix/src/main.rs @@ -0,0 +1,67 @@ +struct Solution(); + +impl Solution { + pub fn spiral_order(matrix: &[&[i32]]) -> Vec { + let mut fr = 0; + let mut lr = matrix.len() - 1; + let mut fc = 0; + let mut lc = matrix.get(0).unwrap().len() - 1; + let mut spiral = Vec::with_capacity((lr + 1) * (lc + 1)); + let cols = lc + 1; + let size = matrix.len() * cols; + loop { + dbg!(fr, lr, fc, lc); + // right + for i in fc..=lc { + spiral.push(matrix[fr][i]); + } + fr += 1; + if spiral.len() == size {} + + // down + for row in fr..=lr { + spiral.push(matrix[row][lc]); + } + lc = lc.saturating_sub(1); + if spiral.len() == size { + break; + } + + // left + if fr <= lr { + for e in (fc..=lc).rev() { + spiral.push(matrix[lr][e]); + } + } + lr = lr.saturating_sub(1); + if spiral.len() == size { + break; + } + + // up + if fc <= lc { + for i in (fr..=lr).rev() { + spiral.push(matrix[i][fc]); + } + } + fc += 1; + if spiral.len() == size { + break; + } + + dbg!(&spiral); + } + + spiral + } +} + +fn main() { + dbg!(Solution::spiral_order(&[ + &[1, 2, 3], + &[4, 5, 6], + &[7, 8, 9] + ])); + + dbg!(Solution::spiral_order(&[&[3], &[2]])); +}