-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_nest.go
160 lines (154 loc) · 3.49 KB
/
parse_nest.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package lem
import (
"fmt"
"strconv"
"strings"
)
func findFirstLineWithTunnel(text []string) int {
for i := 5; i < len(text); i++ {
if strings.Contains(text[i], "-") && !strings.Contains(text[i], "#") {
return i
}
}
return len(text)
}
func nameRoom(name string, room *Room) *Room {
room.Name = name
return room
}
func newRoom(row string) (*Room, bool) {
a := strings.Split(row, " ")
var problem bool
if len(a) != 3 {
fmt.Println("ERROR: Invalid coordinates.")
problem = true
}
x, er1 := strconv.Atoi(a[1])
y, er2 := strconv.Atoi(a[2])
if er1 != nil || er2 != nil {
fmt.Println("ERROR: Invalid coordinates.")
problem = true
}
var room Room
nameRoom(a[0], &room)
room.X = x
room.Y = y
room.Flow = make(map[*Room]bool)
return &room, problem
}
func findRoom(name string, counter int, nest *Nest) *Room {
if counter == 0 {
return nil
}
if nest.Rooms[counter-1].Name == name {
return nest.Rooms[counter-1]
}
counter--
return findRoom(name, counter, nest)
}
func ParseNest(text []string, firstNonCommentLine int) (Nest, bool) {
var nest Nest
startLine := 0
endLine := 0
n := findFirstLineWithTunnel(text)
for i := firstNonCommentLine + 1; i < n; i++ {
switch {
case strings.Contains(text[i], "#"):
case text[i] == "":
default:
r, problem := newRoom(text[i])
if problem {
return nest, true
}
if text[i-1] == "##start" {
r.Start = true
nest.Start = r
startLine = i
if i-2 >= 0 && text[i-2] == "##end" {
endLine = i - 1
}
}
if text[i-1] == "##end" {
r.End = true
nest.End = r
endLine = i
if i-2 >= 0 && text[i-2] == "##start" {
startLine = i - 1
}
}
nest.Rooms = append(nest.Rooms, r)
}
}
diff := endLine - startLine
switch diff {
case 1:
nest.Start = nest.End
return nest, false
case -1:
nest.End = nest.Start
return nest, false
default:
if nest.Start == nil {
fmt.Println("ERROR: No start room found.")
return nest, true
}
if nest.End == nil {
fmt.Println("ERROR: No end room found.")
return nest, true
}
if n == len(text) {
fmt.Println("ERROR: No tunnels.")
return nest, true
}
}
for i := 0; i < len(nest.Rooms); i++ {
for j := n; j < len(text); j++ {
if !(text[j] == "") && !strings.Contains(text[j], "#") {
pair := strings.Split(text[j], "-")
if nest.Rooms[i].Name == pair[0] {
v := findRoom(pair[1], len(nest.Rooms), &nest)
match := false
for _, nb := range nest.Rooms[i].Neighbors {
if nb == v {
match = true
}
}
if !match || (nest.Rooms[i].Start && v.End) || (nest.Rooms[i].End && v.Start) {
nest.Rooms[i].Neighbors = append(nest.Rooms[i].Neighbors, v)
}
}
if nest.Rooms[i].Name == pair[1] {
u := findRoom(pair[0], len(nest.Rooms), &nest)
match := false
for _, nb := range nest.Rooms[i].Neighbors {
if nb == u {
match = true
}
}
if !match || (nest.Rooms[i].Start && u.End) || (nest.Rooms[i].End && u.Start) {
nest.Rooms[i].Neighbors = append(nest.Rooms[i].Neighbors, u)
}
}
}
}
}
for i, ii := range nest.Rooms {
for j, jj := range nest.Rooms {
if ii.Name == jj.Name && j != i {
fmt.Println("ERROR: Duplicated room.")
return nest, true
}
}
for _, jj := range nest.Rooms[i].Neighbors {
if ii == nil || jj == nil {
fmt.Println("ERROR: Link to unknown room.")
return nest, true
}
if jj.Name == ii.Name {
fmt.Println("ERROR: Room links to itself.")
return nest, true
}
}
}
return nest, false
}