summaryrefslogtreecommitdiff
path: root/src/structs.rs
diff options
context:
space:
mode:
authornatrixaeria <upezu@student.kit.edu>2019-12-22 06:21:32 +0100
committernatrixaeria <upezu@student.kit.edu>2019-12-22 06:21:32 +0100
commitdd0ed4cfff1a4af2a518aab399e32174175ed0d8 (patch)
tree124d00db8c17dff68db8fa5b1e4a7f762ea0851f /src/structs.rs
parentc22cbdc401d067cd6cc55c5194255ccc8bd3c71b (diff)
Arrange project structure
Diffstat (limited to 'src/structs.rs')
-rw-r--r--src/structs.rs98
1 files changed, 98 insertions, 0 deletions
diff --git a/src/structs.rs b/src/structs.rs
new file mode 100644
index 0000000..571c79d
--- /dev/null
+++ b/src/structs.rs
@@ -0,0 +1,98 @@
+pub struct StoneWall {
+ rows: Vec<Vec<u32>>,
+}
+
+impl StoneWall {
+ pub fn create_empty(n: u32) -> Self {
+ let n = n as usize;
+ let h = n / 2 + 1;
+ let mut rows = vec![vec![0; n]; h];
+ rows.get_mut(0).map(|r| r[0] = 1);
+ rows.get_mut(1).map(|r| r[1] = 2);
+ Self {
+ rows,
+ }
+ }
+}
+
+pub struct GapHeights {
+ heights: Vec<u32>,
+}
+
+impl GapHeights {
+ pub fn from_heights(heights: Vec<u32>) -> Self {
+ Self { heights }
+ }
+
+ #[allow(dead_code)]
+ 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 add_gap(&mut self, height: u32) {
+ self.add_gap(height)
+ }
+
+ pub fn calculate_row(&self, r: u32, stones: &mut [u32]) {
+ let mut len = 1;
+ let mut i = 0;
+ for &height in self.heights.iter().chain([r].iter()) {
+ if height == r {
+ stones[i] = len;
+ i += 1;
+ len = 0;
+ }
+ len += 1;
+ }
+ }
+
+ pub fn output(&self, n: u32, h: u32) {
+ let mut stones = vec![0; n as usize];
+ let mut toggle = 0;
+ let colors = [
+ "\x1b[31m", "\x1b[32m", "\x1b[33m", "\x1b[34m", "\x1b[35m", "\x1b[36m",
+ ];
+ for row in 0..h {
+ self.calculate_row(row, &mut stones);
+ for &len in stones.iter() {
+ print!("{}", colors[toggle]);
+ toggle = (toggle + 1) % colors.len();
+ for _ in 0..len {
+ print!("◙");
+ }
+ }
+ println!("\x1b[m");
+ }
+ }
+
+ pub fn iter<'a>(&'a self) -> GapIter<'a> {
+ GapIter {
+ gap_heights: self,
+ i: 0,
+ }
+ }
+}
+
+pub struct GapIter<'a> {
+ gap_heights: &'a GapHeights,
+ i: usize,
+}
+
+impl<'a> Iterator for GapIter<'a> {
+ type Item = u32;
+ fn next(&mut self) -> Option<Self::Item> {
+ let i = self.i;
+ self.i += 1;
+ self.gap_heights.heights.get(i).map(|&x| x)
+ }
+}