forked from guillaumebonnot/sandy-explore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (55 loc) · 1.34 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
package main
import (
"fmt"
"map_explorer/point"
"map_explorer/trie"
)
func main() {
// BONUS first we need to find the boundaries, in order to find which type we should use
find_boundaries()
// we determine that should use int16 be -499 < x < 499
explore()
}
// we explore map boundaries
func find_boundaries() {
for i := int16(0); i < 1000; i++ {
if point.New(i, int16(0)).IsMine() {
fmt.Println("The boundaries are : ", i)
return
}
}
}
// we explore them map for real this time
func explore() {
// the tree to store which points we already checked
tree := trie.New()
// the queue to plainify which points we want to check next
queue := make([]*point.Point, 0, 100)
queue = planify(tree, queue, point.New(0, 0))
count := 0
for len(queue) > 0 {
// dequeue
p := queue[0]
queue = queue[1:]
// fmt.Println("Check ", p)
if p.IsMine() {
count++
} else {
for _, neighbour := range p.Neighbours() {
// say hello to your neighbour
queue = planify(tree, queue, neighbour)
}
}
}
fmt.Println("Number of points Sandy can explore (including zero) :", count)
}
func planify(tree *trie.Trie, queue []*point.Point, p *point.Point) []*point.Point {
// check in the tree it has already been planified
_, ok := tree.Search(p.Path())
// already planified
if ok {
return queue
}
tree.Add(p)
return append(queue, p)
}