---
title: "Snail sort"
date: "2022.05.13"
slug: "posts/refactor"
---

I was looking for some file/s on my computer, but instead i found this:
```rs
//main.rs
fn main () {
    let mut res: Vec<i32> = Vec::new();
    let mut pos = "right";
    let mut vec_index = 0;
    let mut a = vec!(
                    vec![ 1,  2,  3,  4,  5, 6],
                    vec![20, 21, 22, 23, 24, 7],
                    vec![19, 32, 33, 34, 25, 8],
                    vec![18, 31, 100, 35, 26, 9],
                    vec![17, 30, 29, 28, 27, 10],
                    vec![16, 15, 14, 13, 12, 11],
                );
                
    while a.len() != 0 {
        if pos == "right" {
            let mut temp_vec = a.clone();
            for j in 0..a[vec_index].len() {
                res.push(a[vec_index][j]);
                temp_vec[vec_index]
                    .retain(|&x| x == a[vec_index][j]);
            }
            temp_vec.retain(|x| x.len() != 0 );
            a = temp_vec;
            pos = "down";
        } else if pos == "down" {
            let mut temp_vec = a.clone();
            res.push(a[vec_index][a[vec_index].len() - 1]);
            temp_vec[vec_index]
                .retain(|&x| x != a[vec_index][a[vec_index].len() - 1]);
            a = temp_vec;
            if vec_index + 1 == a.len() {
                pos = "left";
                continue;
            }
            vec_index += 1;
        } else if pos == "left" {
            let rev_temp: Vec<i32> = a[vec_index]
                                        .to_vec()
                                        .into_iter()
                                        .rev()
                                        .collect();
            for j in 0..rev_temp.len() {
                res.push(rev_temp[j]);
            }
            a.pop();
            pos = "up";
        } else if pos == "up" {
            vec_index -= 1;
            let mut temp_vec = a.clone();
            if vec_index == 0 {
                pos = "right";
                continue;
            } else {
                res.push(a[vec_index][0]);
                temp_vec[vec_index]
                    .retain(|&x| x != a[vec_index][0]);
                a = temp_vec;
                continue;
            }
        }
    }
    
    println!("{:?}", res);
}
```

So...I got distracted instead of looking the file/s that I supposed to find, 
I try to refactor this code 👍.

Anyway this algorithm is called snailsort algorithm
<br/>

```
    SnailSort is an algorithm that takes an array of equal-length sub-arrays and then merges them into a 
    single array in a clockwise spiral, starting from the upper-left hand corner[1].

    ex: 
    [
      [1,2,3],
      [8,9,4],
      [7,6,5],
    ]

    return [1,2,3.4.5.6,7,8,9]
```
    

<div style={{ display: "flex", justifyContent: "center" }}>
    <img src="https://cdn.discordapp.com/attachments/743422487882104837/995679214030291036/unknown.png"/>
</div>

Yeah it's funny.Idk why i worked on snailsort, but i was having fun.
<br/>

anyway here's the refactor [result](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f02de1004ba64cde17255802614fed82).Using Match for the routes and Enum to declare the routes avoiding hard coded way 
like ``pos="left"`` to make it looks cleaner, and i change about those clones fest 
man, damn look at those clones on my first attempt man we naruto now.Using  [VecDeque](https://doc.rust-lang.org/std/collections/struct.VecDeque.html) `VecDeque<slice::Iter<i32>>` 
i can easily pop, append/extend, and other manipulation that i want.

<br/>

aaand i forgor about file/s that i supposed to looking for 💀.

<br/>
<br/>

References:
1. [https://medium.com/@spencerwhitehead7/snail-sort-the-gimmick-sort-goat-310510814eab](https://medium.com/@spencerwhitehead7/snail-sort-the-gimmick-sort-goat-310510814eab)
