-
Notifications
You must be signed in to change notification settings - Fork 0
/
need_for_speed.go
48 lines (41 loc) · 989 Bytes
/
need_for_speed.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
package speed
// 'Car' type struct
type Car struct {
battery int
batteryDrain int
speed int
distance int
}
// 'Track' type struct
type Track struct {
distance int
}
// NewCar creates a new remote controlled car with full battery and given specifications.
func NewCar(speed, batteryDrain int) Car {
return Car{
battery: 100,
batteryDrain: batteryDrain,
speed: speed,
distance: 0,
}
}
// NewTrack created a new track
func NewTrack(distance int) Track {
return Track{
distance: distance,
}
}
// Drive drives the car one time. If there is not enough battery to drive on more time,
// the car will not move.
func Drive(car Car) Car {
if car.battery < car.batteryDrain {
return car
}
car.battery -= car.batteryDrain
car.distance += car.speed
return car
}
// CanFinish checks if a car is able to finish a certain track.
func CanFinish(car Car, track Track) bool {
return car.battery/car.batteryDrain*car.speed >= track.distance
}