-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
59 lines (49 loc) · 991 Bytes
/
report.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
package unter
import "time"
const (
minFee = 250 // ¢
perMile = 250
perHour = 3000
)
// RideFee returns the ride fee in ¢
func RideFee(duration time.Duration, distance float64, shared bool) int {
m := perMile * distance
h := perHour * float64(duration/time.Minute/60)
fee := max(m, h)
fee = max(fee, minFee)
if shared {
fee = 0.9 * float64(fee)
}
return int(fee)
}
func max(a, b float64) float64 {
if a > b {
return a
}
return b
}
type Report struct {
Driver string
NumRides int
Payment int
}
func ByDriver(rides []Ride) []Report {
rs := make(map[string]*Report) // driver -> report
for _, r := range rides {
rp, ok := rs[r.Driver]
if !ok {
rp = &Report{
Driver: r.Driver,
}
rs[r.Driver] = rp
}
rp.NumRides++
duration := r.End.Sub(r.Start)
rp.Payment += RideFee(duration, r.Distance, r.Kind == Shared) - 30
}
reports := make([]Report, 0, len(rs))
for _, rp := range rs {
reports = append(reports, *rp)
}
return reports
}