-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
72 lines (56 loc) · 1.4 KB
/
main.go
File metadata and controls
72 lines (56 loc) · 1.4 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
package main
import (
"encoding/json"
"log"
"strings"
)
func main() {
b := `{"forward":"tiger", "left":{"forward":{"upstairs":"exit"}, "left":"dragon"}, "right":{"forward":
"dead end"}}`
b = `{"forward":"tiger", "left": "ogre", "right":"demon"}`
store := make(map[string]interface{})
err := json.Unmarshal([]byte(b), &store)
if err != nil {
log.Fatal("error while converting json to map!", err)
}
log.Println(FindWay(store))
}
type Route struct {
Route []string
Rest interface{}
}
// FindWay get map[string]interface{} input and return. see main_test for more usage
func FindWay(maze map[string]interface{}) string {
result := findWay(maze)
if len(result) == 0 {
return "Sorry"
} else {
return `["` + strings.Join(result, `","`) + `"]`
}
}
func findWay(maze map[string]interface{}) []string {
var processList []Route
for k, v := range maze {
processList = append(processList, Route{[]string{k}, v})
}
for len(processList) > 0 {
route := processList[0]
if len(processList) == 1 {
processList = []Route{}
} else {
processList = processList[1:]
}
switch route.Rest.(type) {
case string:
if route.Rest.(string) == "exit" {
return route.Route
}
case map[string]interface{}:
tmpMaze := route.Rest.(map[string]interface{})
for k, v := range tmpMaze {
processList = append(processList, Route{append(route.Route, k), v})
}
}
}
return []string{}
}