-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneed_for_speed.go
44 lines (44 loc) · 1.12 KB
/
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
package speed
// TODO: define the 'Car' type struct
type Car struct {
battery int
batteryDrain int
speed int
distance int
}
// NewCar creates a new remote controlled car with full battery and given specifications.
func NewCar(speed, batteryDrain int) Car {
return Car{
speed:speed,
batteryDrain:batteryDrain,
battery:100,
distance:0,
}
}
// TODO: define the 'Track' type struct
type Track struct {
distance int
}
// 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 {
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 {
for i:=car.distance;i<track.distance;i+=car.speed {
if car.battery < car.batteryDrain {
return false
}
car = Drive(car)
}
return true
}