Small Rust scripts

Add simple sierpinski triangle script

+47
+47
sierpinski
··· 1 + #!/usr/bin/env -S cargo eval -- 2 + 3 + // cargo-deps: argh = "0.1" 4 + 5 + use argh::FromArgs; 6 + 7 + const TRIANGLE: char = '▲'; 8 + 9 + #[derive(FromArgs)] 10 + #[argh(description = "Prints a Sierpinski triangle.")] 11 + struct App { 12 + #[argh(positional, description = "size of the Sierpinski triangle.")] 13 + size: Option<u8>, 14 + } 15 + 16 + fn main() { 17 + let App { size } = argh::from_env(); 18 + let size = size.unwrap_or(1); 19 + 20 + let height = 2_usize.pow(size as u32); 21 + let width = (2 * height) - 1; 22 + 23 + let mut bounding_box = vec![vec![0_u16; width]; height]; 24 + bounding_box[0][height - 1] = 1; 25 + 26 + let mut last_row = bounding_box[0].clone(); 27 + for (i, row) in bounding_box.iter_mut().skip(1).enumerate() { 28 + for (j, cell) in row.iter_mut().enumerate() { 29 + let top_left = last_row.get(j - 1).unwrap_or(&0); 30 + let top_right = last_row.get(j + 1).unwrap_or(&0); 31 + 32 + *cell = top_left + top_right; 33 + } 34 + last_row = row.clone(); 35 + } 36 + 37 + for row in bounding_box { 38 + for cell in row { 39 + if cell == 0 || cell % 2 == 0 { 40 + print!(" "); 41 + } else { 42 + print!("{}", TRIANGLE); 43 + } 44 + } 45 + println!(); 46 + } 47 + }