Created
October 21, 2014 15:11
-
-
Save kjvalencik/6b229ea63fe4584aa8c7 to your computer and use it in GitHub Desktop.
This could be solved with linear algebra, but it only took a few minutes to write a brute force script.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Three farmers were selling chickens at the local market. | |
One farmer had 10 chickens to sell, another had 16 chickens to sell, and the last had 26 chickens to sell. | |
In order not to compete with each other, they agreed to all sell their chickens at the same price. | |
But by lunchtime, they decided that sales were not going so well, and they all decided to lower their | |
prices to the same lower price point. By the end of the day, they had sold all their chickens. It turned | |
out that they all collected the same amount of money, $35, from the day's chicken sales. | |
What was the price of the chickens before lunchtime and after lunchtime? | |
*/ | |
var chickens = [10, 16, 26]; | |
var sales = 3500; | |
function checkItem(num, x, y) { | |
for (var i = num; i > 0; i--) { | |
if (x * i + y * (num - i) === sales) { | |
return true; | |
} | |
} | |
} | |
function isSolution(x, y) { | |
return chickens.reduce(function (works, chickens) { | |
return works && checkItem(chickens, x, y); | |
}, true); | |
} | |
function runTest() { | |
for (var i = sales; i > 0; i--) { | |
for (var j = i; j > 0; j--) { | |
if (isSolution(i, j)) { | |
console.log("$" + i / 100, "$" + j / 100); | |
return; | |
} | |
} | |
} | |
} | |
runTest(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment