blob: a7cd4813e0f3bcb3917096b759ca66821803fd5f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
pub struct Wall {
heights: Vec<u32>,
}
impl Wall {
fn create_empty(w: u32) -> Self {
let heights = if w == 0 {
vec![]
} else if w == 1 {
vec![0]
} else {
let mut v = Vec::with_capacity(w as usize);
v.push(0);
v.push(1);
v
};
Self { heights }
}
pub fn calculate_row(&self, r: u32, stones: &mut [u32]) {
let mut len = 0;
let mut i = 0;
for &height in self.heights.iter() {
if height == r {
stones[i] = len;
i += 1;
len = 0;
}
len += 1;
}
}
pub fn output(&self, w: u32, h: u32) {
let mut stones = vec![0; w as usize];
for row in 0..h {
self.calculate_row(row, &mut stones);
for &len in stones.iter() {
if len <= 1 {
print!("◼");
} else if len > 1 {
print!("◢");
for _ in 0..(len-2) {
print!("◼");
}
print!("◣");
}
}
println!("");
}
}
}
pub struct Solver {
pub n: u32, pub h: u32, pub w: u32,
}
impl Solver {
pub fn new(n: u32) -> Self {
let h = n / 2 + 1;
let w = h * (n - 1);
Self {
n, h, w
}
}
pub fn solve(&mut self) -> Wall {
Wall::create_empty(self.w)
}
}
|