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