This repository has been archived by the owner on Oct 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (84 loc) · 1.62 KB
/
main.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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"bufio"
"fmt"
"strings"
)
var input = `1 2 3
8 9 4
7 6 5`
func main() {
parsed := parse(input)
output := decode(parsed)
for _, out := range output {
fmt.Print(string(out) + " ")
}
}
func parse(input string) [][]rune {
var matrix [][]rune
scanner := bufio.NewScanner(strings.NewReader(input))
for scanner.Scan() {
line := scanner.Text()
lineScanner := bufio.NewScanner(strings.NewReader(line))
lineScanner.Split(bufio.ScanWords)
var lineArr []rune
for lineScanner.Scan() {
word := lineScanner.Text()
char := []rune(word)[0]
lineArr = append(lineArr, char)
}
matrix = append(matrix, lineArr)
}
return matrix
}
const (
Right = 0
Down = 1
Left = 2
Up = 3
)
func decode(input [][]rune) []rune {
var result []rune
width := len(input[0])
height := len(input)
direction := Right
for {
switch direction {
case Right:
result = append(result, input[0]...)
// Removing top row
input = input[1:]
height--
case Left:
for i := width - 1; i >= 0; i-- {
result = append(result, input[height-1][i])
}
// Remove bottom row
input = input[:height-1]
height--
case Down:
for _, line := range input {
result = append(result, line[width-1])
}
// Remove right column
for i, line := range input {
input[i] = line[:width-1]
}
width--
case Up:
for i := height - 1; i >= 0; i-- {
result = append(result, input[0][i])
}
// Remove left column
for i, line := range input {
input[i] = line[1:]
}
width--
}
direction = (direction + 1) % 4
if width == 0 || height == 0 {
break
}
}
return result
}