-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnail.cs
More file actions
76 lines (64 loc) · 1.78 KB
/
Snail.cs
File metadata and controls
76 lines (64 loc) · 1.78 KB
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
71
72
73
74
75
76
// Link to kata: https://www.codewars.com/kata/521c2db8ddc89b9b7a0000c1
using System;
using System.Collections.Generic;
public class SnailSolution
{
public enum Directions
{
UP = 0,
DOWN,
LEFT,
RIGHT
}
public static int[] Snail(int[][] array)
{
if(array[0].Length == 0) return new int[] {};
List<int> result = new List<int>();
int top = 0;
int bottom = array.GetLength(0) - 1;
int left = 0;
int right = array.GetLength(0) - 1;
Directions direction = Directions.RIGHT;
while(top <= bottom && left <= right)
{
switch(direction)
{
case Directions.RIGHT:
for(int i = top; i <= right; i++)
{
result.Add(array[top][i]);
}
top++;
direction = Directions.DOWN;
break;
case Directions.DOWN:
for(int i = top; i <= bottom; i++)
{
result.Add(array[i][right]);
}
right--;
direction = Directions.LEFT;
break;
case Directions.LEFT:
for(int i = right; i >= left; i--)
{
result.Add(array[bottom][i]);
}
bottom--;
direction = Directions.UP;
break;
case Directions.UP:
for(int i = bottom; i >= top; i--)
{
result.Add(array[i][left]);
}
left++;
direction = Directions.RIGHT;
break;
default:
break;
}
}
return result.ToArray();
}
}