forked from ociule/codeeval
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_square.go
More file actions
105 lines (94 loc) · 2.06 KB
/
find_square.go
File metadata and controls
105 lines (94 loc) · 2.06 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
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
package main
import "fmt"
import "log"
import "bufio"
import "os"
import "strings"
import "strconv"
import "math"
import "sort"
type Vertex struct {
X int
Y int
}
func parse_test(test string) []Vertex {
svertices := strings.Split(test, "),")
vertices := make([]Vertex, 4)
for ix, v := range svertices {
v = strings.TrimSpace(v)
v = strings.TrimPrefix(v, "(")
v = strings.TrimSuffix(v, ")")
xy := strings.Split(v, ",")
x, _ := strconv.Atoi(xy[0])
y, _ := strconv.Atoi(xy[1])
vertex := Vertex{x, y}
vertices[ix] = vertex
}
return vertices
}
func get_len(a, b Vertex) float64 {
return math.Sqrt(math.Pow(float64(a.X-b.X), 2) + math.Pow(float64(a.Y-b.Y), 2))
}
const e = 1.0 / 1000000000
func test_float_eq(a, b float64) bool {
return (math.Abs(a - b)) < e
}
func test_sq_tri(points []Vertex) (Vertex, bool) {
a, b, c := points[0], points[1], points[2]
ab := get_len(a, b)
bc := get_len(b, c)
ac := get_len(a, c)
sides := []float64{ab, bc, ac}
sort.Float64s(sides)
longest := sides[2]
sqrt2 := math.Sqrt(float64(2.0))
if longest == 0 || sides[0] != sides[1] || !test_float_eq(sides[0]*sqrt2, longest) {
return Vertex{}, false
} else {
switch longest {
case ab:
return c, true
case ac:
return b, true
case bc:
return a, true
}
return Vertex{}, false
}
}
func test_square(points []Vertex) bool {
sq_vertex, sq_tri := test_sq_tri(points[0:3])
if !sq_tri {
return false
} else {
var others []Vertex
a, b, c := points[0], points[1], points[2]
switch sq_vertex {
case a:
others = []Vertex{b, c}
case b:
others = []Vertex{a, c}
case c:
others = []Vertex{a, b}
}
others = append(others, points[3])
sq_vertex, sq_tri = test_sq_tri(others)
if sq_tri && sq_vertex == points[3] {
return true
} else {
return false
}
}
}
func main() {
file, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
//'scanner.Text()' represents the test case, do something with it
fmt.Println(test_square(parse_test(scanner.Text())))
}
}