Skip to content

Instantly share code, notes, and snippets.

@Ashaba
Created April 16, 2019 16:16
Show Gist options
  • Save Ashaba/4f0cbf996624539268e82f65c9161220 to your computer and use it in GitHub Desktop.
Save Ashaba/4f0cbf996624539268e82f65c9161220 to your computer and use it in GitHub Desktop.
package challenge;
import java.util.HashMap;
/**
* You have 10,000/= bob, and you need to buy exactly 100 animals:
* - cows @1000/-
* - pigs @200/-
* - chicken @50/-
*
* 1. You must have at least one of each animal
* 2. You must use up all Ksh. 10K
* 3. You must buy exactly 100 animals
*
* How many animals can be bought
Equations
* x + y + z = 100
* 1000x + 200y + 50z = 10000
*
*
*/
public class AnimalChallenge {
private static void calc(int[] prices, int[] animals, int totalCost, int totalAnimals){
HashMap coefficients = getCoefficients(prices[0], prices[1], prices[2], totalCost, animals[0],animals[1], animals[2], totalAnimals);
int numberOfCows;
int numberOfPigs = 0;
int numberOfChicken = 0;
int x = (int) coefficients.get("x");
int y = (int) coefficients.get("y");
int c = (int) coefficients.get("c");
// loop through the possible values of the eliminated variable while using the created functions
// to compute the other values and test whether they pass the given condition
for(numberOfCows = 1; numberOfCows <totalCost/prices[0]; numberOfCows++){
numberOfPigs = getY(x, y, c, numberOfCows);
numberOfChicken = getZ(animals[0], animals[1], animals[2], totalAnimals, x, y, c, numberOfCows);
int totalNoOfAnimals = numberOfCows + numberOfPigs + numberOfChicken;
int totalPriceOfAnimals = numberOfCows * prices[0] + numberOfPigs * prices[1] + numberOfChicken * prices[2];
if(totalNoOfAnimals == totalAnimals && totalPriceOfAnimals == totalCost){
break;
}
}
System.out.println("Number of Cows: "+ numberOfCows);
System.out.println("Number of Pigs: "+ numberOfPigs);
System.out.println("Number of Chicken: "+ numberOfChicken);
}
private static HashMap<String, Integer> getCoefficients(int x1, int y1, int z1, int c1, int x2, int y2, int z2, int c2){
HashMap<String , Integer> coefficients = new HashMap<>();
int xValue = z2 * x1 - z1 * x2;
int yValue = z2 * y1 - z1 * y2;
int cValue = z2 * c1 - z1 * c2;
coefficients.put("x", xValue);
coefficients.put("y", yValue);
coefficients.put("c", cValue);
return coefficients;
}
// compute the value of Y for a given value of the eliminated variable
private static int getY(int x, int y, int c, int a){
return (c-x*a)/y;
}
// compute the value of Z for a given value of the eliminated variable
private static int getZ(int x1, int y1, int z1, int c1, int x, int y, int c, int a){
return (c1 * y - c * y1 + y1 * a * x - x1 * a * y) / (z1 * y);
}
public static void main(String[] args){
int[] animalPrices = {1000, 200, 50};
int[] animalNames = {1, 1, 1};
int totalCost = 10000;
int totalAnimals = 100;
calc(animalPrices, animalNames, totalCost, totalAnimals);
}
}
@Ashaba
Copy link
Author

Ashaba commented Apr 16, 2019

Number of Cows: 1
Number of Pigs: 27
Number of Chicken: 72

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment