Your friend Jack is a picky eater. He only likes certain foods, and he only likes to eat a certain amount of them each day. Despite this, Jack has very strict calorie and macronutrient goals that he strives to meet with his diet. To complicate things further, Jack's cravings change day by day, and he will only eat the foods that he wants, up to a certain amount. Your goal is to write a program that Jack can use to enter his goals and his cravings on a given day, and be shown a combination of those foods that most closely matches his dietary requirements.
Program Input - Accept the following data in any convenient format for your programming language. All integers, except for food names which are strings. Carbs, fats, and protein are given in grams.
Jack's Dietary Requirements:
- A Calorie Target
- A Carbohydrate Target
- A Fat Target
- A Protein Target
A list of foods (of any length), containing the following info:
- The name of the food
- Calories per serving
- Carbohydrates per serving
- Fat per serving
- The maximum number of servings that Jack is willing to consume in a day
Program Output - Text, or any data structure that can assocaite the name of a food with the optimal number of servings
- A list of the number of servings (integers) of each food that Jack should have to get closest to his diet requirements. (It is ok for the number of calories, protein, fat, or carbs to be above or below the requirements. Only return the closest solution).
The absolute difference score of a combination of foods will be scored as follows (lower scores are better):
- 1 point for each calorie difference from Jack's goal. Ex, if the total number of calories for a given number of servings for each food adds up to 1800, and Jack's goal is 2000 calories, add 200 points to the score.
- 10 points for each gram difference from Jack's goal. Ex, if a combination of foods and servings contains a total of 160 grams of protein, and Jack's goal is 150, add 100 points to the score.
Your program should return the solution with the lowest absolute difference score - a number of servings for each of Jack's craved foods. If multiple combinations have the same score, return any of them.
Example
Input:
{
'goals':[
'calories': 2000,
'carbs': 200,
'fats': 120,
'protein': 150
]
'cravings':[
'chocolate':[
'calories': 130,
'carbs': 14,
'fats': 8,
'protein': 10,
'max_servings': 99
]
'beans': [
'calories': 80,
'carbs': 6,
'fats': 2,
'protein': 18,
'max_servings': 99
]
]
}
Output:
chocolate 15
beans 0
This is code-golf, so the shortest code wins.