-
Notifications
You must be signed in to change notification settings - Fork 6
/
horse_dual.go
55 lines (43 loc) · 876 Bytes
/
horse_dual.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
package main
import (
"fmt"
"github.com/glendc/cgreader"
"math"
"sort"
)
type intArray []int
func (s intArray) Len() int {
return len(s)
}
func (s intArray) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s intArray) Less(i, j int) bool {
return s[i] < s[j]
}
func main() {
cgreader.RunStaticPrograms(
cgreader.GetFileList("../../input/horse_dual_%d.txt", 3),
cgreader.GetFileList("../../output/horse_dual_%d.txt", 3),
true,
func(input <-chan string, output chan string) {
var n int
fmt.Sscanf(<-input, "%d", &n)
horses := make([]int, n)
for i := range horses {
fmt.Sscanf(<-input, "%d", &horses[i])
}
sort.Sort(intArray(horses))
D := math.MaxInt32
for i := 1; i < n; i++ {
x := horses[i-1] - horses[i]
if x < 0 {
x *= -1
}
if x < D {
D = x
}
}
output <- fmt.Sprintf("%d", D)
})
}