Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

closed as off-topic by n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳, Martin Ender, TimWolla, Peter Taylor, Chris Jester-Young Mar 25 '14 at 17:15

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions without an objective primary winning criterion are off-topic, as they make it impossible to indisputably decide which entry should win." – Chris Jester-Young
If this question can be reworded to fit the rules in the help center, please edit the question.

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. – Level River St Mar 25 '14 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... – Martin Ender Mar 25 '14 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. – Martin Ender Mar 25 '14 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. – Martin Ender Mar 25 '14 at 13:01
Rounding definitely needs to be defined. – Bill Woodger Mar 25 '14 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 =
input = "1000x90
2500x200" # temporary debugging tool

food = 100000

input ={|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 have a good feeling about this now
# ... I'm evil

loop {
  daysNobodyDied += 4
  food -= foodForOnePerson
  if food >= 0
    daysNobodyDied += 1
    daysSomeoneDied += 1

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


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

share|improve this answer
I thought the output was " many days your city lasted before someone died", not until everyone died. – Geobits Mar 25 '14 at 13:01
@Geobits "For every day your city goes without anyone dying, you get one point." – Doorknob Mar 25 '14 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... – Bill Woodger Mar 25 '14 at 14:15
BTW, is this guy doing anything beside just hanging around? I didn't know the guys at Wolfram|Alpha red whatif-XKCD ;). – ɐɔıʇǝɥʇuʎs Mar 25 '14 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. – Howard Mar 25 '14 at 15:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.