Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Banana programming challenge: Utilitarianism

Name: Adam 2013-01-19 23:31

Hello everyone. My name is Adam. For as long as I can remember, my sister and I have loved eating bananas. We do not share the same banana eating habbits however. I typically like eating the bananas earlier, when they are more green. My sister likes to eat the bananas later. You might think to yourself that we have ourselves a nice setup. I can buy a bunch of bananas and we can share, me eating the bananas earlier ones and her eating the later ones. But it turns out to be not so simple.

Suppose that the value I get from eating a banana can be given by ceil(.22*d^3-2.7*d^2+7.7*d+3.8) for integer values of d between 0 and 6 inclusive and d represents the number of days passed since purchasing the bananas. Also suppose that the value my sister gets from eating a banana is given by a graph that is symmetrical to mine about 3. As far as the cost of the bananas, the value of a dollar is worth 5 units of value to us.

On days that a banana is not eaten either of us, our little brother takes one for himself. This is to be avoided as he does not contribute money towards the bananas.

Maximize the combined value my sister and I can get from a bunch of bananas using the number of bananas bought and when they are eaten as variables. Bananas can only be bought at most one bunch per week and at most each person can eat one banana in a given day. The number of bananas consumed by my sister and I cannot differ by more than 1 for a given week. Bananas are sold in bunches of between 6-8 inclusive. Assume each banana weighs the same (.48 lb) and cost $1.42/lb.

Name: Anonymous 2013-01-19 23:32

i like adam

Name: Anonymous 2013-01-19 23:36

This can be solved with one line of Haskell.

Name: Anonymous 2013-01-19 23:39

The little brother sticks bananas in his anus while shitposting on /prog/.

Name: Anonymous 2013-01-19 23:46

>>4
Who would derive value from shitposting on /prog/?

Name: Anonymous 2013-01-19 23:56

This can be solved with one line of Haskell.

Name: Anonymous 2013-01-20 0:18

restoring...

Name: Anonymous 2013-01-20 5:01

lb
lel imperial units

Name: Anonymous 2013-01-20 8:25

minimum value of a banana : 3.4 units
max value : 10.2 units
Buy 8, you eat first two at night (day 0 & 1), second two in the morning (day 2 & 3)
approx val for 4 banana : 36 units (~8 units each)
sis gets first two in the morning (day 2 & 3), second two at night (day 4 & 5)
Little bro misses out because there are none left aww poor little guy
total val ~72 units
total cost $5.45
val / cost 13.2 units

Name: Anonymous 2013-01-20 9:09

This can be solved with three characters of Symta.

Name: Anonymous 2013-01-20 9:29

This can be solved with my dubs.

Name: Anonymous 2013-01-20 9:57

check 'em

Name: Anonymous 2013-01-20 10:10

so the room will be empty

Name: Anonymous 2013-01-20 11:30

Is this utilitarianism, or just your way of shoehorning in a long word you recently learnt?

Name: Anonymous 2013-01-20 14:58

Right in the Culver

Name: Anonymous 2013-01-20 15:12

>>9
you would just subtract the price of the bananas times 5 from your current value. Dont divide nigger.

Also nice proof. where is the code? Do you know how the ceil function even works? And why do you differentiate between day and night when the problem says nothing of the sort.

Name: Anonymous 2013-01-20 16:01

>>1
Ask Stack Overflow.

Name: Anonymous 2013-01-20 17:10

This is already solved automatically when you start an APL interpreter.

Name: Anonymous 2013-01-20 18:49

>>17
Stack Overflow is gay.

Name: Anonymous 2013-01-20 19:03

>>19
Exactly.

Name: Anonymous 2013-01-20 20:28

>>16 code? ^^ i just ran an anonymous function in octave, and plotted it... the rest was manual (but not hard)

Name: Anonymous 2013-01-20 20:58

I thought this would be an easy simulation... I give up

Name: Anonymous 2013-01-20 21:24

>Also suppose that the value my sister gets from eating a banana is given by a graph that is symmetrical to mine about 3.
I don't get this part.

Name: Anonymous 2013-01-20 21:35

yeah, it's worded a little funny.. Reflective symmetry i think he meant. So if he is 'd' and his sis 'g' then g = 6-d

Name: Anonymous 2013-01-20 23:10

>>23,24
If adam is 'd' and sis is 'g' then g[i] = d[6-i]

Also, I can't get the permutations right for every possible scenario.

Name: Anonymous 2013-01-20 23:56

>>25 i like the way you're thinking =) i'll have a think now too =D

Name: Anonymous 2013-01-21 0:02

>>26
Beware, it might hurts the first time.

Name: Anonymous 2013-01-21 0:05

#1) assume that banana's are bought on day 0, and that at the end of day 6, any left are thrown away.. (one (the first?) combination) // no overlapping weeks // never more than 8 banana's in one week?
#2) On every day, unless there are no banana's left, at least one banana, and no more than two, are eaten.

Name: Anonymous 2013-01-21 0:06

it's trinary?

Name: Anonymous 2013-01-21 0:08

no wait, base-4?

Name: Anonymous 2013-01-21 0:12

hm, pseudo-base 4, or trinary-Plus... (making shit up ^^)
trinary will be easier... =)

Name: Anonymous 2013-01-21 0:14

So you get 3^8 + 3^7 + 3^6 + ...combinatorics..?

Name: Anonymous 2013-01-21 0:20

(7*3^6 + 6*3^5 + 5*3^4)+ (one day of two banana's eaten?)
(5*6 *3^4 + 4*5 *3^3 + 3*4 *3^2) + (two days of two..?)
any good?

Name: Anonymous 2013-01-21 0:27

Buy 8 bananas. You eat 4 in {1 2 3 4}, your sister eats 4 in {3 4 5 6}.

Name: Anonymous 2013-01-21 0:32

5*6 / 2 hey..?

+ (5*6/2 *3^4 + 4*5/2 *3^3 + 3*4/2 *3^2)  [two days of two..?]

+ ((4*3*2 / 3*2) * 3^2) + (5 * 3) + 1 [three days?]

+ 1 [four days?]

Name: Anonymous 2013-01-21 0:37

>>34
Excuse me, I forgot to start from index 0 for the sister. I mean {2 3 4 5}

Reasoning:

(define (c d) (ceiling (+ (* .22 (* d d d)) (* -2.7 (* d d)) (* 7.7 d) 3.8)))
(map c (range 7))
'(4.0 10.0 11.0 9.0 6.0 3.0 1.0)


The rest is really trivial to confirm after seeing the graph:
    Banana price is less than a dollar, which is less than 5 points.
    Maximum amount of bananas is 8
    Sisters graph is (reverse (map c (range 7)))
    Since picking the maximum x from the list is the same for the reverse of the list you should split the bananas evenly
    Assume we take maximum bananas
    8/2 = 4
    Minimum of the maximum 4 in the list is 6.0
    6.0 > 5 | We should take 8 bananas because each provides positive value
    Pick the maximum 4 of my list and my sisters list
    {1 2 3 4} and {2 3 4 5}

Name: Anonymous 2013-01-21 0:39

I give up. I suck at programming and don't know why I bother. I'm doomed to a life of toilet scrubbing.

This is what I left off with:


#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

typedef char bool;
#define true 1
#define false 0



float banana_value(char array[14]);
void initialize_values(float array1[7], float array2[7]);
void get_permutations(char array[][14]);
bool valid_case(char permutation[14], int bananas);

float value_adam[7], value_sister[7];


int main(void) {
  int n = (int) pow(2, 14); //number of permutations
  float total_value = 0, max_value = 0, money_value;
  int bananas_purchased, i, j, max_bananas;

  char permutations[n][14], max_permutation[14];
 
 
  initialize_values(value_adam, value_sister);
  get_permutations(permutations);
 
  for(bananas_purchased = 6; bananas_purchased < 9; bananas_purchased++) {
    money_value = bananas_purchased * .48 * 1.42;
    for(i = 0; i < n; i++) {
      if(!valid_case(permutations[i], bananas_purchased))
        continue;
     
      total_value = banana_value(permutations[i]) - money_value;
      if(total_value > max_value) {
        max_value = total_value;
        max_bananas = bananas_purchased;
        for(j=0;j<14;j++)
          max_permutation[j] = permutations[n][j];
      }
    }
  }
 
  printf("Max Value: %f\nBananas Purchased: %d\n", max_value, bananas_purchased);
  for(i = 0; i < 14; i++)
    if(max_permutation[i])
      putchar('1');
    else
      putchar('0');
 
 
  putchar('\n');
  exit(EXIT_SUCCESS);
}

void initialize_values(float array1[7], float array2[7]) {
  int d = 0;
  for(; d < 7; d++)
    array1[d] = ceil(.22*pow(d, 3) - 2.7*pow(d, 2) + 7.7*d + 3.8);
 
  for(d = 0; d < 7; d++)
    array2[d] = array1[6-d];
}

void get_permutations(char array[][14]) {
  int i[14] = {0};
  int j = -1, k, a = 0;
  for(;i[0]<2;i[0]++)
    for(;i[1]<2;i[1]++)
      for(;i[2]<2;i[2]++)
        for(;i[3]<2;i[3]++)
          for(;i[4]<2;i[4]++)
            for(;i[5]<2;i[5]++)
              for(;i[6]<2;i[6]++)
                for(;i[7]<2;i[7]++)
                  for(;i[8]<2;i[8]++)
                    for(;i[9]<2;i[9]++)
                      for(;i[10]<2;i[10]++)
                        for(;i[11]<2;i[11]++)
                          for(;i[12]<2;i[12]++)
                            for(;i[13]<2;i[13]++)
                              for(j++, k=0; k < 14; k++)
                                array[j][k] = i[k];
                        
}

bool valid_case(char permutation[14], int bananas) {
  int n_adam = 0, n_sister = 0; //number of bananas eaten by each
  int i = 0, bananas_left = bananas;

  for(; i < 14; i += 2) {
    if(permutation[i]) //banana eaten by adam
      if(--bananas_left < 0)
        return(false);
      else
        n_adam++;
   
    if(permutation[i+1]) //banana eaten by sister
      if(--bananas_left < 0)
        return(false);
      else
        n_sister++;
    
    if(!permutation[i] && !permutation[i+1]) //lil bro steals a banana
      if(bananas_left > 0) //but only if there is one left to steal!
        bananas_left--;
  }
 
  if(abs(n_adam - n_sister) > 1)
    return(false);
   
  return(true);
     
}

float banana_value(char permutation[14]) {
  float v = 0;
  int i = 0;
 
  for(; i < 14; i += 2)
    if(permutation[i])
      v += value_adam[i/2];
     
  for(i = 0; i < 14; i += 2)
    if(permutation[i])
      v+= value_sister[i/2];
     
  return(v);
}

Output:
Max Value: 48.228798
Bananas Purchased: 7
00111100111100

For my permutation strings, the order is:
day 0 adam, day 0 sister, day 1 adam, day 2 sister, ...

Name: Anonymous 2013-01-21 0:40

>>36
Not a valid proof.

Name: Anonymous 2013-01-21 0:44

>>38
Where does it go wrong?

Name: Anonymous 2013-01-21 0:46

..did i mess it up? i think 3 days / w 8 bananas is probably off...

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List