Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code Challenge: Snacky Friends, a donkey, and Apples
#1
You'll find my solution for this challenge (coded with BAM) here.

The gist of the challenge:

Quote:Once upon a time, 3 friends bought apples and carried them on a donkey.
When night came, they decided to sleep in the forest.

The first friend woke up hungry at midnight, so he divided the apples into three equal shares.
The one extra remaining apple, he gave it to the donkey.  He then ate his share and went back to sleep.

A while later, the second friend woke up hungry, so he divided the remaining apples into three equal shares.
The one extra remaining apple, he gave it to the donkey.  He then ate his share and went back to sleep.

A while later, the third friend woke up hungry, so he divided the remaining apples into three equal shares.
The one extra remaining apple, he gave it to the donkey.  He then ate his share and went back to sleep.

In the morning, the 3 friends woke up hungry, so they divided the remaining apples into three equal shares.
The one extra remaining apple, they gave it to the donkey.  They then ate each of their share.

How many apples did they initially buy, and how many apples did each of them eat?
Reply
#2
Code: (Select All)
For i = 1 To 4000
    If i Mod 3 = 1 Then
        i2 = i - Int(i / 3) - 1 '1st
        If i2 Mod 3 = 1 Then
            i3 = i2 - Int(i2 / 3) - 1 '2nd
            If i3 Mod 3 = 1 Then
                i4 = i3 - Int(i3 / 3) - 1 ' 3rd
                If i4 Mod 3 = 1 Then
                    i5 = i4 - Int(i4 / 3) - 1 '4th
                    If i5 Mod 3 = 0 Then
                        Print i; "="; i2 / 2 + i5 / 2; "+"; i3 / 2 + i5 / 2; "+"; i4 / 2 + i5 / 2; "+"; 4
                        Exit For
                    End If
                End If
            End If
        End If
    End If
Next

This problem is similar to 5 Sailors, Coconuts and a Monkey at Rosetta Code. Find the minimum number to satisfy the conditions set by problem, I learned later at Syntax Bomb.
b = b + ...
Reply
#3
With the code I wrote, the first possible solution for number of apples bought: 79

Friend 3 ate a total of 18 apples.
Friend 2 ate a total of 24 apples.
Friend 1 ate a total of 33 apples.

The donkey ate 4 apples.

Friend 1 is a pig.

I'm not entirely convinced I got it right, so I look forward to confirmation one way or the other via other approaches.
Reply
#4
Okay try this.

My first version was a compact, concise little thing similar to @bplus's, but then I reread your challenge and realized I wasn't giving all the information required.

D'oh!!  Chuck it in the #%@&-it bucket and start with fresh ingredients:
(unusually for me, there is no stunt programming or early optimization on this one... just keepin' it simple)
(this program makes no assumptions e.g. about how many apples the donkey eats; it just performs the calculations implied by the puzzle)


Code: (Select All)
Code: (Select All)
Dim C, T, DONKEYEATEN, F1EATEN, F2EATEN, F3EATEN, ALLEAT As Integer


Print "Total", "FatBoy", "Friend2", "Friend3", "Donkey"

For C = 1 To 1000
    T = C
    DONKEYEATEN = 1 'give the donkey an apple so he won't rat us out
    T = T - 1
    If (T Mod 3) = 0 Then
        F1EATEN = T / 3 'friend #1 aka "fatboy".
        T = T - F1EATEN

        DONKEYEATEN = DONKEYEATEN + 1 'give the donkey an apple so he won't rat us out
        T = T - 1
        If (T Mod 3) = 0 Then
            F2EATEN = T / 3
            T = T - F2EATEN

            DONKEYEATEN = DONKEYEATEN + 1 'give the donkey an apple so he won't rat us out
            T = T - 1
            If (T Mod 3) = 0 Then
                F3EATEN = T / 3
                T = T - F3EATEN

                DONKEYEATEN = DONKEYEATEN + 1 'the donkey is sick of apples by now
                T = T - 1
                If (T Mod 3) = 0 Then
                    ALLEAT = T / 3
                    F3EATEN = F3EATEN + ALLEAT
                    F2EATEN = F2EATEN + ALLEAT
                    F1EATEN = F1EATEN + ALLEAT

                    Print C, F1EATEN, F2EATEN, F3EATEN,  DONKEYEATEN

                End If
            End If
        End If
    End If
Next


Possible solutions up to 1000:
   
Reply
#5
While it appears the donkey got the short end of the stick that night, write a program, for the next day, showing how many apples came out of their _ _ _.

Pete Big Grin
Shoot first and shoot people who ask questions, later.
Reply
#6
Testing 79

79 / 3 = 26 R 1 79 - 26 - 1 = 52

52 / 3 = 17 R 1 52 - 17 - 1 = 34

34 / 3 = 11 R 1 34 - 11 - 1 = 22

22 / 3 = 7 R 1 so 7 more for each friend and donkey gets the 4 Remainders.

26 + 7 = 33 1st
17 + 7 = 24 2nd
11 + 7 = 18 3rd
+4 Donkey
= 79 It works alright I stand corrected

Where did I go wrong?
Code: (Select All)
For i = 1 To 4000
    If i Mod 3 = 1 Then
        i2 = i - Int(i / 3) - 1 '1st
        If i2 Mod 3 = 1 Then
            i3 = i2 - Int(i2 / 3) - 1 '2nd
            If i3 Mod 3 = 1 Then
                i4 = i3 - Int(i3 / 3) - 1 ' 3rd
                If i4 Mod 3 = 1 Then
                    i5 = i4 - Int(i4 / 3) - 1 '4th
                    'If i5 Mod 3 = 0 Then
                    Print i; "="; i2 / 2 + i5 / 2; "+"; i3 / 2 + i5 / 2; "+"; i4 / 2 + i5 / 2; "+"; 4
                    Exit For
                    'End If
                End If
            End If
        End If
    End If
Next

Aha! i5 mod 3 = 0 was not required!

Well it wouldn't be right to be first AND be correct ;-))

Quote:Friend 1 is a pig.
ha! Friend 1 was the earliest bird for the worm laden apples Smile
b = b + ...
Reply
#7
(08-04-2024, 08:54 AM)Pete Wrote: While it appears the donkey got the short end of the stick that night, write a program, for the next day, showing how many apples came out of their _ _ _.

Pete Big Grin

Code: (Select All)
print 0; "apples came out that any reasonable, well fed human might eat."
b = b + ...
Reply
#8
(08-04-2024, 05:20 AM)JRace Wrote: Okay try this.

[...snip...]

Man, I love code that has comedic Easter eggs in 'em !!!

When I graduated from university eons ago, I got hired and immediately thrown into a project that was long over budget and late delivering.

Four months later, the project was delivered (but buggy), and I was the only team member kept on to eliminate the bugs.  The other two Programmer's, the two analysis, the technical architect and the project manager all left.

Not long after,  I find this gem of a comment in the code: "Completely non-standard way of designing a form.  See my analyst."

Extra funny to me because of the friction between the 4GL Windows Programmer's and the COBOL Mainframe background analysts.

All of that aside: thanks!   Nice to compare output results to validate one's own program with somebody else's work.

(08-04-2024, 08:54 AM)Pete Wrote: While it appears the donkey got the short end of the stick that night, write a program, for the next day, showing how many apples came out of their _ _ _.

Pete Big Grin

Insta-thought: When one comes from a place where the men are men and the donkeys are nervous, the donkeys can only hope to get the short end of any stick late at night ...
Reply
#9
(08-04-2024, 10:47 AM)bplus Wrote: ha! Friend 1 was the earliest bird for the worm laden apples  Smile

The earliest bird may get the worm, but it is the second mouse that gets the cheese.
Reply
#10
LOL! oh man I had to think about that for more than second, then all the funnier! Smile
b = b + ...
Reply




Users browsing this thread: 5 Guest(s)