---
# System prepended metadata

title: 'Snail [4 kyu]'
tags: [C++, CodeWars]

---

# Snail [4 kyu]

[Snail](https://www.codewars.com/kata/521c2db8ddc89b9b7a0000c1)
4 kyu

## Solution
```cpp=
/**
*** Author: R-CO
*** E-Mail: daniel1820kobe@gmail.com
*** Date: 2020-08-15
**/
#include <vector>

std::vector<int> snail(const std::vector<std::vector<int>> &snail_map) {
  if (snail_map[0].empty()) {
    return {};
  }
  
  std::vector<int> output;
  
  const int n = snail_map.size();
  const int n_half = n >> 1;
  int loop_count = 0;
  int loop_max = (n % 2 == 0) ? n_half : n_half + 1;
  
  while (loop_count < loop_max) {
    int i = 0;
    int j = 0;
    
    j = loop_count;
    for (i = loop_count; i < n - loop_count; ++i) {
      output.push_back(snail_map[j][i]);
    }
    
    if (loop_count >= n_half) {
      break;
    }
    
    i = n - loop_count - 1;
    for (j = loop_count + 1; j < n - loop_count; ++j) {
      output.push_back(snail_map[j][i]);
    }
    
    if (loop_count >= n_half) {
      break;
    }
    
    j = n - loop_count - 1;
    for (i = n - loop_count - 2; i >= loop_count; --i) {
      output.push_back(snail_map[j][i]);
    }
    
    i = loop_count;
    for (j = n - loop_count - 2; j > loop_count; --j) {
      output.push_back(snail_map[j][i]);
    }
    
    ++loop_count;
  }
  
  return output;
}

```

## Result

PASS


###### tags: `CodeWars` `C++`
