A Snail's Journey
#1
A member on SoloLearn posted an interesting challenge that inspired me to create a demo app that solved the challenge. Here is the challenge presented by that member (in their own words):


A Snail's Challenge
Each day the snail climbs up A meters on a tree with H meters in height. At night it goes down B meters.

Write a program which takes 3 inputs: H, A, B, and calculates how many days it will take for the snail to get to the top of the tree.

Assume H > A > B

Solve this challenge without using conditions or loops.



Here is my solution to the challenge, written in C++:

Code:
/*  Written by: Walter Whitman  a.k.a.  Waltersmind  a.k.a.  The Joyful Programmer
   Created:  April 1, 2018
   Website:  http://www.TheJoyfulProgrammer.com
   License:  Public Domain
*/

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
   double H, A, B, DaysToClimb;

   cout << "Story:\n\n"
           "Each morning, a snail climbs up a tree of a specific height trying to get to the\n"
           "top. In the evening, it gives up and starts climbing back down but falls asleep.\n\n"
           "By providing this demo with the height of the tree, how far the snail will climb\n"
           "each day, and far down it will descend before it falls asleep, I will magically\n"
           "give you the exact day the snail will reach the top of the tree.\n\n"
           "Please note: No accepted distance measurement is implied or assumed in this demo.\n"
           "You can pretend you are entering the distances in inches, feet, meters, or any\n"
           "other measurement system that you prefer. Please provide only decimal numbers.\n"
           "Please also assume that the height of the tree is greater than the distance the\n"
           "snail travels up, which is also greater than the distance the snail climbs down."
           "\n\nHow tall of a tree does the snail have to climb? ";

   cin >> H;

   cout << "\nHow far up the tree will the snail climb each day before it gives up? ";

   cin >> A;

   cout << "\nHow far down the tree will the snail descend before it falls asleep? ";

   cin >> B;

   /*  To get the distance the snail has moved up the tree in one day, we need to subtract the
       distance the snail moves down the tree from how far up it climbs. Then we can divide the
       height of the tree by the actual distance the snail made it up the tree at the end of
       the day. But this is where things can get tricky if we don't pay attention. On the last
       day of the climb, the snail never goes down the tree, so we need to account for that. We
       do this by adding the distance the snail moves down the tree in a day to the final value
       and then round down the answer to the lowest integer.
   */
   DaysToClimb = floor((H / (A - B)) + B);

   cout << "\nThe snail will reach the top of the tree on day " << DaysToClimb << "." << endl;

   return 0;
}

Here is a screenshot:



I have attached the source code to the project as well in a *.ZIP file. The source code was written in the Code::Blocks IDE, so the attached compressed file comes with a Code::Blocks project file.

For those new to C++, download the latest Code::Blocks IDE at http://www.codeblocks.org/ and follow these instructions:

Since I already included the Code::Blocks project file with the source code in the attached compressed file, there are no extra setup steps to take. The Code::Blocks project file is already is setup to provide the follow features:
  • -std=c++1z  (This option tells the compiler to use the C++ standard ver 17)
  • -static-libgcc  (This option links the GNU libgcc library statically - compiles the GCC library into the final executable so it will run without the *.DLL files)
  • -static-libstdc++  (This option links the standard C++ library statically - compiles the standard C++ library into the final executable so it will run without the *.DLL files)
  • -m64  (Tells the compiler to compile the project into a 64-Bit executable)
  • -Wall  (Enable all common compiler warnings)
  • -0s  (Optimize generated code for size - we don't need the option for speed since there are no speed issues in this demo)




If you want or need to compile to a 32-Bit application instead of a 64-Bit, go to the top menu and click on "Project", then click on "Properties", as seen in this screenshot:




You will see a new window pop up like:




Stay on the first tab, "Project Settings" and click on the button at the bottom right, "Project's Build Options...". You will then get another popup window with all the compiler options, like in the following screenshot:




You may need to resize the window to bring more options into view.

If you need to change the options to compile to 32-Bit applications, then simply click on the option "[-m32]". The "[-m64]" option will automatically become unchecked. Now just click on "Ok" in both windows to close them out.

On a side note, it took me less than 30 seconds to come up with the equation to the problem after I read the challenge. It was too easy in my mind.

A funny thing is, I started looking at other members code for this challenge on the SoloLearn website, and I was shocked to see some of them using loops, conditionals, and functions (the first two inside the last) to solve this simple math problem.


Attached Files .zip   TJP - A Snails Journey - ver 1.zip (Size: 2.02 KB / Downloads: 3)
Dedicated to empowering computer programming hobbyists, tinkerers, amateurs, and enthusiasts.
profile for Walter Whitman at Stack Overflow, Q&A for professional and enthusiast programmers


Reply
#2
I'll let QB64 write my C code...


Code:
PRINT "Each day the snail climbs up A meters on a tree with H meters in height. At night it goes down B meters."

PRINT "Write a program which takes 3 inputs: H, A, B, and calculates how many days it will take for the snail to get to the top of the tree."

PRINT "Assume H > A > B": PRINT

INPUT "Height of tree:"; h: PRINT
INPUT "Snail ascent:"; a: PRINT
INPUT "Snail descent:"; b: PRINT

days = h / (a - b)

PRINT "Days for the snail to climb to tree top:"; days
Reply
#3
There is also block about interpreters:

what a haack is this now

Please correct the following errors before continuing:

You are not allowed to post links in this forum.
Reply
#4
Given the right H, A, and B the snail can do it in 2nd day MINIMUM!

One example  H = 3, A = 2, B = 1

Day 1   Height = 2
Night 1 Height =  2-1 = 1
Day 2   Height =  1 + 2 = 3 the top of the tree!

Who needs a computer program for that?
B += x
Reply
#5
Code:
' Test Pete's ABH.bas SmallBASIC 0.12.11 (B+=MGA) 2018-04-01
PRINT "Each day the snail climbs up A meters on a tree with H meters in height. At night it goes down B meters."

PRINT "Write a program which takes 3 inputs: H, A, B, and calculates how many days it will take for the snail to get to the top of the tree."

PRINT "Assume H > A > B": PRINT

INPUT "Height of tree:"; h: PRINT
INPUT "Snail ascent:"; a: PRINT
INPUT "Snail descent:"; b: PRINT

days = h / (a - b)

PRINT "Days for the snail to climb to tree top:"; days



Hi Pete,

The tricky part of this problem is that snail can reach top before it starts crawl back down like on the 2nd day for H = 3, A = 2 and B = 1


Attached Files Thumbnail(s)

B += x
Reply
#6
Quote:DaysToClimb = floor((H / (A - B)) + B);

3 / (2-1) + 1 = 4 yikes!
B += x
Reply
#7
Formula:

Max Climb or H = (A - B) * (Days - 1) + A, Max climb at dusk.
Look at it as the sum of all the progress made up to the morning of the day the snail makes it's final ascent then add that for max distance traveled in day before the backslide starts again at dusk.

Algebraic Manipulation:
(H - A) / (A - B) = Days - 1

Therefore:
Days = (H - A) / (A - B) + 1

check:  (3-2) / (2-1) + 1 = 2   correct!


check:
H = 15, A = 1, B = .5

14 / .5 + 1 = 29 days

Might like a computer program to verify that with loops and conditions.
B += x
Reply
#8
Code to verify my formula:
Code:
' Test Pete's ABH.bas SmallBASIC 0.12.11 (B+=MGA) 2018-04-01
' modified to check my formula for days
PRINT "Each day the snail climbs up A meters on a tree with H meters in height. At night it goes down B meters."

PRINT "Write a program which takes 3 inputs: H, A, B, and calculates how many days it will take for the snail to get to the top of the tree."

PRINT "Assume H > A > B": PRINT

Repeat
  INPUT "Height of tree:"; h: PRINT
  INPUT "Snail ascent:"; a: PRINT
  INPUT "Snail descent:"; b: PRINT
  
  climb = 0 : days = 0
  while climb < h
    days = days + 1
    climb = climb + a
    ? "Day ";days;" climbed to ";climb
    if climb >= h then exit
    climb = climb - b
    ? "Night back slide to ";climb
  wend
  
  PRINT "Days for the snail to climb to tree top: "; days
  ? "Days = (h - a) / (a - b) + 1 says days should be ";int((h - a) / (a - b)) + 1
  ?: input "Try another set of h, a and b numbers? just enter, any other quits ";again
until len(again)


Attached Files Thumbnail(s)

B += x
Reply
#9
Hi Walter,

Sorry to burst bubble with snail formula (unless April Fool's joke) but you sure are making learning C++ very tempting.
I was wondering if C++ had a free distribution. Now I see I could get it from Code Blocks?

Oh wait, looks like Code Blocks is some sort of editor like Notepad++ that can be setup to run PL's like C++.

Also this cheapskate wants to know if any fees were involved with SoloLearn because I think Khan Academy is free.
aprilfools:www.khanacademy.org
(test post a link for Aurel)

I am glad to be out of school institutional learning but just learning by hanging out at forums is kind of slow though way more fun!


Attached Files Thumbnail(s)

B += x
Reply
#10
(04-01-2018, 10:04 PM)bplus Wrote:
Code:
' Test Pete's ABH.bas SmallBASIC 0.12.11 (B+=MGA) 2018-04-01
PRINT "Each day the snail climbs up A meters on a tree with H meters in height. At night it goes down B meters."

PRINT "Write a program which takes 3 inputs: H, A, B, and calculates how many days it will take for the snail to get to the top of the tree."

PRINT "Assume H > A > B": PRINT

INPUT "Height of tree:"; h: PRINT
INPUT "Snail ascent:"; a: PRINT
INPUT "Snail descent:"; b: PRINT

days = h / (a - b)

PRINT "Days for the snail to climb to tree top:"; days



Hi Pete,

The tricky part of this problem is that snail can reach top before it starts crawl back down like on the 2nd day for H = 3, A = 2 and B = 1

Unless it's a female snail, which takes longer because it has to stop to ask for directions!

Pete Big Grin
Reply
#11
THIS IS ONLY A TEST...
Reply
#12
Interesting, Stealth joined in 2014 and just got around to posting something, but, just a test.
B += x
Reply
#13
@Bplus,

StealthMode is my regular account that I use to test this site as a regular "Registered Member". I usually don't post with that account, but I do, I always delete the posts. Now that we are talking about StealthMode, I will not be able to delete that post since people would get confused as to what is going on.


Ok. I do not know why the "no link" thing is happening, but I am working on trying to figure it out. There are no settings in the admin section that I can find, nor are there any permissions in the category section that can be set to cause this. Now I have to hack the website to find out.
Dedicated to empowering computer programming hobbyists, tinkerers, amateurs, and enthusiasts.
profile for Walter Whitman at Stack Overflow, Q&A for professional and enthusiast programmers


Reply
#14
http://www.thejoyfulprogrammer.com

Ok... It is allowing the administrator account to post links, but not registered users... Hummm....
Dedicated to empowering computer programming hobbyists, tinkerers, amateurs, and enthusiasts.
profile for Walter Whitman at Stack Overflow, Q&A for professional and enthusiast programmers


Reply
#15
http://www.TheJoyfulProgrammer.com
This is a test account created by the Administrator Waltersmind.
Reply
#16
Ok all...

I fixed the issue. Apparently there was a plugin that I installed when I started this forum that was causing the issue. While I am not sure why the plugin is now causing a problem after all this time, I resolved the issue. I didn't uninstall it because I didn't want to run into issues or spend a lot of time fixing my fix, so I did the next best thing. I modified the plugins code to bypass the check for links, videos, and images, as those are items I will always allow on this forum.

I was able to verify that my fix worked with my StealthMode account, as seen above.
Dedicated to empowering computer programming hobbyists, tinkerers, amateurs, and enthusiasts.
profile for Walter Whitman at Stack Overflow, Q&A for professional and enthusiast programmers


Reply
#17
Snail crawling up the tree calendar:

Day 1: Climb up 1 distance (0.0 + 1.0) = 1.0, came back down 0.5 distance = (1.0 - 0.5) = 0.5
Day 2: Climb up 1 distance (0.5 + 1.0) = 1.5, came back down 0.5 distance = (1.5 - 0.5) = 1.0
Day 3: Climb up 1 distance (1.0 + 1.0) = 2.0, came back down 0.5 distance = (2.0 - 0.5) = 1.5
Day 4: Climb up 1 distance (1.5 + 1.0) = 2.5, came back down 0.5 distance = (2.5 - 0.5) = 2.0
Day 5: Climb up 1 distance (2.0 + 1.0) = 3.0, came back down 0.5 distance = (3.0 - 0.5) = 2.5
Day 6: Climb up 1 distance (2.5 + 1.0) = 3.5, came back down 0.5 distance = (3.5 - 0.5) = 3.0
Day 7: Climb up 1 distance (3.0 + 1.0) = 4.0, came back down 0.5 distance = (4.0 - 0.5) = 3.5
Day 8: Climb up 1 distance (3.5 + 1.0) = 4.5, came back down 0.5 distance = (4.5 - 0.5) = 4.0
Day 9: Climb up 1 distance (4.0 + 1.0) = 5.0, came back down 0.5 distance = (5.0 - 0.5) = 4.5
Day 10: Climb up 1 distance (4.5 + 1.0) = 5.5, came back down 0.5 distance = (5.5 - 0.5) = 5.0
Day 11: Climb up 1 distance (5.0 + 1.0) = 6.0, came back down 0.5 distance = (6.0 - 0.5) = 5.5
Day 12: Climb up 1 distance (5.5 + 1.0) = 6.5, came back down 0.5 distance = (6.5 - 0.5) = 6.0
Day 13: Climb up 1 distance (6.0 + 1.0) = 7.0, came back down 0.5 distance = (7.0 - 0.5) = 6.5
Day 14: Climb up 1 distance (6.5 + 1.0) = 7.5, came back down 0.5 distance = (7.5 - 0.5) = 7.0
Day 15: Climb up 1 distance (7.0 + 1.0) = 8.0, came back down 0.5 distance = (8.0 - 0.5) = 7.5
Day 16: Climb up 1 distance (7.5 + 1.0) = 8.5, came back down 0.5 distance = (8.5 - 0.5) = 8.0
Day 17: Climb up 1 distance (8.0 + 1.0) = 9.0, came back down 0.5 distance = (9.0 - 0.5) = 8.5
Day 18: Climb up 1 distance (8.5 + 1.0) = 9.5, came back down 0.5 distance = (9.5 - 0.5) = 9.0
Day 19: Climb up 1 distance (9.0 + 1.0) = 10.0, came back down 0.5 distance = (10.0 - 0.5) = 9.5
Day 20: Climb up 1 distance (9.5 + 1.0) = 10.5, came back down 0.5 distance = (10.5 - 0.5) = 10.0
Day 21: Climb up 1 distance (10.0 + 1.0) = 11.0, came back down 0.5 distance = (11.0 - 0.5) = 10.5
Day 22: Climb up 1 distance (10.5 + 1.0) = 11.5, came back down 0.5 distance = (11.5 - 0.5) = 11.0
Day 23: Climb up 1 distance (11.0 + 1.0) = 12.0, came back down 0.5 distance = (12.0 - 0.5) = 11.5
Day 24: Climb up 1 distance (11.5 + 1.0) = 12.5, came back down 0.5 distance = (12.5 - 0.5) = 12.0
Day 25: Climb up 1 distance (12.0 + 1.0) = 13.0, came back down 0.5 distance = (13.0 - 0.5) = 12.5
Day 26: Climb up 1 distance (12.5 + 1.0) = 13.5, came back down 0.5 distance = (13.5 - 0.5) = 13.0
Day 27: Climb up 1 distance (13.0 + 1.0) = 14.0, came back down 0.5 distance = (14.0 - 0.5) = 13.5
Day 28: Climb up 1 distance (13.5 + 1.0) = 14.5, came back down 0.5 distance = (14.5 - 0.5) = 14.0
Day 29: Climb up 1 distance (14.0 + 1.0) = 15.0,

@Bplus,

Your right! I see what I did wrong. The formula should be DaysToClimb = floor((H / (A - B)) - B);

The last part should of been "- B" instead of "+ B". I simply typed the wrong sign key. This is how easy mistakes can be made in programming.

Here is the updated C++ code:

Code:
/*  Written by: Walter Whitman  a.k.a.  Waltersmind  a.k.a.  The Joyful Programmer
    Created:  April 1, 2018
    Website:  http://www.TheJoyfulProgrammer.com
    License:  Public Domain
*/

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double H, A, B, DaysToClimb;

    cout << "Story:\n\n"
            "Each morning, a snail climbs up a tree of a specific height trying to get to the\n"
            "top. In the evening, it gives up and starts climbing back down but falls asleep.\n\n"
            "By providing this demo with the height of the tree, how far the snail will climb\n"
            "each day, and far down it will descend before it falls asleep, I will magically\n"
            "give you the exact day the snail will reach the top of the tree.\n\n"
            "Please note: No accepted distance measurement is implied or assumed in this demo.\n"
            "You can pretend you are entering the distances in inches, feet, meters, or any\n"
            "other measurement system that you prefer. Please provide only decimal numbers.\n"
            "Please also assume that the height of the tree is greater than the distance the\n"
            "snail travels up, which is also greater than the distance the snail climbs down."
            "\n\nHow tall of a tree does the snail have to climb? ";

    cin >> H;

    cout << "\nHow far up the tree will the snail climb each day before it gives up? ";

    cin >> A;

    cout << "\nHow far down the tree will the snail descend before it falls asleep? ";

    cin >> B;

    /*  To get the distance the snail has moved up the tree in one day, we need to subtract the
        distance the snail moves down the tree from how far up it climbs. Then we can divide the
        height of the tree by the actual distance the snail made it up the tree at the end of
        the day. But this is where things can get tricky if we don't pay attention. On the last
        day of the climb, the snail never goes down the tree, so we need to account for that. We
        do this by subtracting the distance the snail moves down the tree in a day to the final
        value and then round down the answer to the lowest integer.
    */
    DaysToClimb = floor((H / (A - B)) - B);

    cout << "\nThe snail will reach the top of the tree on day " << DaysToClimb << "." << endl;
    cin >> A;
    return 0;
}


Here is an updated screenshot of the running application:



Attached Files .zip   TJP - A Snails Journey - ver 1.zip (Size: 2.02 KB / Downloads: 2)
Dedicated to empowering computer programming hobbyists, tinkerers, amateurs, and enthusiasts.
profile for Walter Whitman at Stack Overflow, Q&A for professional and enthusiast programmers


Reply
#18
Hmm... I wonder if our formulas are equivalent? BRB!
B += x
Reply
#19
Back to drawing board for BOTH of us: H = 3.5, A = 2, B = 1 takes 3 days, we predict 2.

I think we need to add ceilings NOT floors to our formulas.


Attached Files Thumbnail(s)

B += x
Reply
#20
Yep, both our formulas fare better with CEIL. But Walter's fails with H = 5.5, A = 3, B = 1
Code:
' Test Pete's ABH.bas SmallBASIC 0.12.11 (B+=MGA) 2018-04-01
' modified to check my formula for days
PRINT "Each day the snail climbs up A meters on a tree with H meters in height. At night it goes down B meters."

PRINT "Write a program which takes 3 inputs: H, A, B, and calculates how many days it will take for the snail to get to the top of the tree."

PRINT "Assume H > A > B": PRINT

Repeat
 INPUT "Height of tree:"; h: PRINT
 INPUT "Snail ascent:"; a: PRINT
 INPUT "Snail descent:"; b: PRINT
 
 climb = 0 : days = 0
 while climb < h
   days = days + 1
   climb = climb + a
   ? "Day ";days;" climbed to ";climb
   if climb >= h then exit
   climb = climb - b
   ? "Night back slide to ";climb
 wend
 
 PRINT "Days for the snail to climb to tree top: "; days
 ? "Days = ceil((h-a)/(a-b)) + 1 says days should be ";ceil((h - a) / (a - b)) + 1
 ? " Walter's Days = ceil(h/(a-b)- b) which would be ";ceil(h/(a-b)- b)
 ?: input "Try another set of h, a and b numbers? just enter, any other quits ";again
until len(again)


Attached Files Thumbnail(s)

B += x
Reply