-
Notifications
You must be signed in to change notification settings - Fork 0
/
snail.go
42 lines (31 loc) · 768 Bytes
/
snail.go
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
package kata
func Snail(snailMap [][]int) []int {
result := []int{}
if len(snailMap) == 0 {
return result
}
for len(snailMap) > 0 {
// Add first row to result
result = append(result, snailMap[0]...)
// Remove first row
snailMap = snailMap[1:]
// if there is no more rows, break
if len(snailMap) == 0 {
break
}
// Rotate 90 degrees backwards
snailMap = rotate(snailMap)
}
return result
}
func rotate(snailMap [][]int) [][]int {
// Create new map
rotatedMap := make([][]int, len(snailMap[0]))
// Rotate 90 degrees backwards
for i := 0; i < len(snailMap); i++ {
for j := 0; j < len(snailMap[i]); j++ {
rotatedMap[len(snailMap[i])-j-1] = append(rotatedMap[len(snailMap[i])-j-1], snailMap[i][j])
}
}
return rotatedMap
}