Skip to content

Instantly share code, notes, and snippets.

@HamzaAnis
Last active November 8, 2017 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HamzaAnis/c471068ea89bab8da8e6b5eaf0144967 to your computer and use it in GitHub Desktop.
Save HamzaAnis/c471068ea89bab8da8e6b5eaf0144967 to your computer and use it in GitHub Desktop.
HackerRank competition
/*Shaheryar is a civil engineer and he is working on a site. His work is to excavate (digg) a tunnel he has been provided with three different capacity of tractor trollies suppose x,y,z every time he will be provided with n kg's of mud to fill the three trollies his work is to fill the trolleys according to their capacity. he want to maximize the performance by filling all the trolleys according to thier capacity and once all the trolleys are fully fill only then he will shift the mud from one place to another.
Input Format
Three trolleys with different capacities are 12,45,78. Shaheryaar must fill the trolleys simultaneously. 2 first time 2 kg's of mud is provided which should be put in first trolley with 12kg's of capacity, now as two kg is filled only 10 kg is left. 11 second time 11 kg's of mud is provided he will put 10 kg in first trolley and 1 kg in second as first trolley is full after putting 10 kg. 122 third time 122 kg's of mud is provided 44 kg should be put in second trolley and the rest of the 78 should be put in third.
Constraints
1 ≤ n ≤ 30000
n will contain between 1 and 30000, inclusive
Output Format
2,0,0 only 2 kg is put in first trolley and the rest of the two are empty which is denoted by 0 12,1,0 next time first trolley is filled upto its capacity and the rest of the mud is put in second trolley 12,45,78all the three trolleys have been filled.
Sample Input 0
12,45,78
2
11
122
Sample Output 0
2,0,0
12,1,0
12,45,78*/
package main
import (
"fmt"
"strconv"
)
func main() {
tractors := [3]string{}
fmt.Scanln(&tractors[0], &tractors[1], &tractors[2])
capacityInt := []int{}
for _, i := range tractors {
value, err := strconv.Atoi(i)
if err != nil {
panic(err)
}
capacityInt = append(capacityInt, value)
}
var mud [3]int
fmt.Scan(&mud[0])
fmt.Scan(&mud[1])
fmt.Scan(&mud[2])
filling := []int{0, 0, 0}
fillingNumberB := 0
fillingNumberM := 0
for i := 0; i < 3; i++ {
if capacityInt[fillingNumberB] >= mud[i] {
capacityInt[fillingNumberB] = capacityInt[fillingNumberB] - mud[i]
filling[fillingNumberB] = mud[fillingNumberM]
} else {
var tobeFilled = mud[i] - capacityInt[fillingNumberB]
var remaining = mud[i] - tobeFilled
filling[fillingNumberB] += remaining
capacityInt[fillingNumberB] -= remaining
filling[fillingNumberB+1] += tobeFilled
capacityInt[fillingNumberB+1] -= tobeFilled
fillingNumberB++
}
fmt.Printf("%d,%d,%d\n", filling[0], filling[1], filling[2])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment