summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornatrixaeria <upezu@student.kit.edu>2019-12-20 02:45:59 +0100
committernatrixaeria <upezu@student.kit.edu>2019-12-20 02:45:59 +0100
commit104d5004bf269e2261f956291b416d60d3a270e0 (patch)
treeecbe4b265f7944a1c55c49d7adb64620f178bc67
parent8a01c383dd3dda3bdf1d53bad543f53dac330ae7 (diff)
Create solver and wall data structures
-rw-r--r--src/main.rs6
-rw-r--r--src/solvers.rs70
2 files changed, 75 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs
index 4e19447..53137e4 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,7 @@
+mod solvers;
+
fn main() {
- println!("Hello, babel!");
+ let mut solver = solvers::Solver::new(2);
+ let wall = solver.solve();
+ wall.output(solver.w, solver.h);
}
diff --git a/src/solvers.rs b/src/solvers.rs
new file mode 100644
index 0000000..a7cd481
--- /dev/null
+++ b/src/solvers.rs
@@ -0,0 +1,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)
+ }
+}
+