# Ration your integers and feed the people [closed]

It is wartime, and rationing is taking place in certain regions of your country. Your cities' region is one such region. The city chooses to give everyone the same amount, but in reality, larger people need more food. Chances are, a 200lb man is going to be eating more than that 7lb baby over there. The city, realizing that some people need more food, hires you, their best computer programmer, to solve the issue. They want you to write a program that will divvy up the food appropriately.

Being a smart programmer, you have developed a formula that gives an approximation of how much someone will eat based on their weight.

EAT = (WEIGHT - 1) / 20 + 1

For example, a 124 pound man will eat 7.125 units of food. Knowing that the food is served in packages of 1, and it would be irrational to cut them up, you can round the food units.

Each day, every person eats that many food units. If somebody goes for five days straight with less than that much food per day, they die of starvation.

You will receive population data in this form: Population*Weight, so if you receive 700*180, you know that their are 700 180lb men in the city. Your city has 100,000 units of food stored away.

For every day your city goes without anyone dying, you get one point. Whoever has the most points wins. Make sure you output how many days your city lasted before someone died.

Here is the input:

1000x90 2500x80 3000x150 2500x200

Edit: So, the rule "For every day your city goes without anyone dying, you get one point" was terribly abused. Rule change here: if anybody dies, then game over. No more counting.

• I like it. Those who habitually eat too much (and therefore are obese) get to carry on eating too much. This is exactly the way bureaucracy works (especially with annual budgets.) +1. Commented Mar 25, 2014 at 11:57
• This is really neat. Just a few things: is the separator x or * or up to us? Any restrictions on the kind of input? Does it have to be from stdin or could it be a function argument? Does it have to be a \n delimited string or could it be an array of strings? What rounding should we use? And what's the output? Just the number of days? Or instructions on how to distribute food each day? Also some simple solved test cases might be useful... Commented Mar 25, 2014 at 12:17
• Thinking about it, isn't the solution actually quite trivial? To keep everyone alive for the first five days, I need to feed each person their required ration on day 5, which costs me 70550 units assuming "normal" rounding. The next 4 days no one will starve, but on day 10 I don't have another 70550 units to avoid someone dying. So 9 days is the maximum. Please correct me if I'm wrong. Commented Mar 25, 2014 at 12:49
• @Doorknob I agree that "For every day your city goes without anyone dying, you get one point. Whoever has the most points wins." could be interpreted as you did it. But "Make sure you output how many days your city lasted before someone died." really seems to mean that the OP was getting at how long you can go without anyone dying at all. In any case that doesn't change that the solution is quite trivial and not subject to any interesting optimisation. Commented Mar 25, 2014 at 13:01
• Rounding definitely needs to be defined. Commented Mar 25, 2014 at 14:10

# Ruby, 100003 days/points (~274 years), 5 days before someone dies

For every day your city goes without anyone dying, you get one point.

I shall abuse this rule as much as possible!

Make sure you output how many days your city lasted before someone died.

Ok, I did. You didn't say anything about that affecting your score.

#input = STDIN.read
input = "1000x90
2500x80
3000x150
2500x200" # temporary debugging tool

food = 100000

input = input.split.map{|x| x.split('x').map &:to_i }
onlyPersonToFeedWeight = input.sort_by! {|a|
a[1] # weight of person
}.first[1] # find the lightest weighing person
foodForOnePerson =  ((onlyPersonToFeedWeight - 1) / 20.0 + 1).ceil # EAT = (WEIGHT - 1) / 20 + 1

# you can see where this is going

daysNobodyDied = 0
daysSomeoneDied = 0

# first kill off all these overweight people
daysNobodyDied += 4
food -= foodForOnePerson
daysSomeoneDied += 1 # it's only all but one person in the city, don't worry

# ... I'm evil

loop {
daysNobodyDied += 4
food -= foodForOnePerson
if food >= 0
daysNobodyDied += 1
else
daysSomeoneDied += 1
break
end
}

puts "Survived for #{daysNobodyDied} days"
puts "There were #{daysSomeoneDied} days in which someone died"
puts "The first person died on day 5"


Output:

Survived for 100003 days
There were 2 days in which someone died
The first person died on day 5


I suggest you read the comments in the code first; they're quite amusing. ;) (Code is intentionally verbose.)

The strategy is simple:

• On the first five days, only feed the lightest person (on the fifth day). Then everyone else dies so you don't have to worry about them.
• Continue to starve this poor man with no friends or family by only feeding him once every five days until you run out of food.
• The man will probably die before you run out of food since it will now last 274 years.

Does this make me evil or something?

Oh, and by the way, if he was lying down the whole time, he'd have a better than even chance that a bird would poop in his mouth. (Assuming the birds didn't starve.) Thanks for the... interesting fact, @Synthetica :-P

• I thought the output was "...how many days your city lasted before someone died", not until everyone died. Commented Mar 25, 2014 at 13:01
• @Geobits "For every day your city goes without anyone dying, you get one point." Commented Mar 25, 2014 at 13:02
• On mature reflection, your answer is growing on me. Upvoted even. Realised the question as it stands is not about any actual programming, but just coding a program correctly. So why not? Let me find my copy of Soylent Green and see if I can get some ideas... Commented Mar 25, 2014 at 14:15
• BTW, is this guy doing anything beside just hanging around? wolframalpha.com/input/?i=100003%20days I didn't know the guys at Wolfram|Alpha red whatif-XKCD ;). Commented Mar 25, 2014 at 15:09
• Following your argument you can put the score to infinity. The poster didn't specify that you should stop counting when the last one died. That's even more ridiculous and shows that it is a non-answer. Commented Mar 25, 2014 at 15:18