-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfootball.go
66 lines (57 loc) · 1.47 KB
/
football.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 (
"strings"
"strconv"
"fmt"
"io/ioutil"
"os"
)
func LoadResults(filename string) []Result {
_contents, _ := ioutil.ReadFile(filename)
raw := string(_contents)
lines := strings.Split(raw, "\n")
var _results []Result
for _, line := range lines {
if len(line) > 0 {
_results = append(_results, ParseResult(line))
}
}
return _results
}
func ParseResult(line string) *Result {
awayIndex := strings.Index(line, " - ") + 3
resultIndex := awayIndex + strings.Index(line[awayIndex:], " ") + 1
goals := strings.Split(line[resultIndex:], ":")
_int, _ := strconv.Atoi(goals[0])
_int1, _ := strconv.Atoi(goals[1])
return Result{line[:awayIndex - 3], line[awayIndex:resultIndex - 1], [...]int{_int, _int1}}
}
func CalculatePoints(results []Result, team string) int {
accumulator := 0
for _, result := range results {
accumulator += ResultPoints(team, result)
}
return accumulator
}
func ResultPoints(team string, result Result) int {
if result.Host == team && result.Goals[0] > result.Goals[1] || result.Away == team && result.Goals[0] < result.Goals[1] {
return 3
} else if result.Goals[0] == result.Goals[1] && (result.Host == team || result.Away == team) {
return 1
} else {
return 0
}
}
type Result struct {
Host string
Away string
Goals [2]int
}
func main() {
if len(os.Args) < 3 {
fmt.Println("usage: football <stats-file> <team>")
} else {
results := LoadResults(os.Args[1])
fmt.Println(CalculatePoints(results, os.Args[2]))
}
}