Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions solutions/go/need-for-speed/2/need_for_speed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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,
}
}

type Track struct{
distance int
}
// NewTrack creates 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 one more time,
// the car will not move.
func Drive(car Car) Car {
if car.battery >= car.batteryDrain{
car.distance += car.speed
car.battery -= car.batteryDrain
}
return car
}
// CanFinish checks if a car is able to finish a certain track.
func CanFinish(car Car, track Track) bool {
maxDistance := ((car.battery/car.batteryDrain)*car.speed)
return maxDistance >= track.distance
}