forked from dhartunian/1brcgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseline.go
62 lines (56 loc) · 1.03 KB
/
baseline.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
package main
import (
"bufio"
"fmt"
"math"
"os"
"slices"
"strconv"
"strings"
)
type record struct {
min float64
max float64
sum float64
count float64
}
func main() {
f, err := os.Open("measurements.txt")
if err != nil {
panic(err)
}
defer func() {
err := f.Close()
if err != nil {
panic(err)
}
}()
temps := make(map[string]*record)
var cities []string
s := bufio.NewScanner(f)
for s.Scan() {
line := s.Text()
cityAndTemp := strings.Split(line, ";")
if temps[cityAndTemp[0]] == nil {
temps[cityAndTemp[0]] = &record{
min: math.MaxFloat64,
max: -math.MaxFloat64,
}
cities = append(cities, cityAndTemp[0])
}
i, err := strconv.ParseFloat(cityAndTemp[1], 10)
if err != nil {
panic(err)
}
r := temps[cityAndTemp[0]]
r.min = min(r.min, i)
r.max = max(r.max, i)
r.sum = r.sum + i
r.count = r.count + 1
}
slices.Sort(cities)
for _, city := range cities {
record := temps[city]
fmt.Printf("%s=%.1f/%.1f/%.1f\n", city, record.min, record.sum/record.count, record.max)
}
}