52
\$\begingroup\$

Background

There's a common riddle that goes something like this:

A snail is at the bottom of a 30 foot well. Every day the snail is able to climb up 3 feet. At night when they sleep, they slide back down 2 feet. How many days does it take for the snail to get out of the well?

The intuitive answer is

30 days, because the snail climbs at 1 foot per day for 30 days to reach the top,

but actually the answer is

28 days, because once the snail is 27 feet in the air (after 27 days), they will simply climb the remaining 3 feet to the top on the 28th day.

Challenge

This challenge generalizes this riddle. Given three positive integers as input, representing the total height, the climb height, and the fall height, return the number of days it will take to climb out of the well.

If the snail cannot climb out of the well, you may return 0, return a falsy value, or throw an exception. You may also write code that will halt if and only if a solution exists.

If you wish, you may take the fall height as a negative integer.

Test Cases

(30,  3,  2) -> 28
(84, 17, 15) -> 35
(79, 15,  9) -> 12
(29, 17,  4) -> 2
(13, 18,  8) -> 1
( 5,  5, 10) -> 1
( 7,  7,  7) -> 1
(69,  3,  8) -> None
(81, 14, 14) -> None

Scoring

This is , so the shortest answer in each language wins.

\$\endgroup\$
7

43 Answers 43

1
2
1
\$\begingroup\$

Jelly, 10 bytes

ẋ;\m2S€<i0

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Jelly, 8 bytes

ẋ+\<i0HĊ

Try it online!

How it Works

ẋ+\<i0HĊ - main link. The inputs are a list and an int, e.g. 3,-2 and 30
ẋ          - repeat the first input a number of times equal to the second input
               e.g. 3,-2,3,-2,3,-2,3,-2,...3,-2 (60 elements total)
 +\        - cumulative sum, e.g. 3,1,4,2,5,3,6,4,7,5,8,...32,30
   <       - less than (the second element)
    i0     - get the index of the first 0
      HĊ   - halve and round up
\$\endgroup\$
1
\$\begingroup\$

Google Sheets, 43 Bytes

Worksheet cell function that takes input from cells A1:C1 and outputs to the calling cell

=if(B1>C1,-int((B1-A1)/(B1-C1)-1),int(A1=B1

Excel, 45 Bytes

Same as above, but formatted for MS Excel

=If(B1>C1,-Int((B1-A1)/(B1-C1)-1),Int(A1=B1))
\$\endgroup\$
2
  • \$\begingroup\$ Excel solution does not seem to handle cases where climb distance = well depth. (5,5,10) (7,7,7) \$\endgroup\$
    – Wernisch
    Jul 10, 2017 at 10:59
  • \$\begingroup\$ @Wernisch, good catch! I've corrected this by replacing 0 in the if statement with a INT(A1=B1) call \$\endgroup\$ Jul 10, 2017 at 12:18
1
\$\begingroup\$

bash, 39 bytes

echo $((d=$2-$3,d>0?$1/d-$3/d:$2/$1%2))

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Kotlin, 70 bytes

fun s(h:Int,c:Int,f:Int):Int=if(h>c)if(c>f)1+s(h-c+f,c,f)else 0 else 1

Try it online!

\$\endgroup\$
1
\$\begingroup\$

@yBASIC, 45 bytes

@__
_=_+_%-_#__=__+!.GOTO(@_)+"_"*(_>_%)@_?__

The language has no support for input so you have to set variables _, _%, and _# to the well depth, fall distance, and climb distance, respectively. (Is this allowed?)

Try it online! (Experimental)

\$\endgroup\$
1
\$\begingroup\$

GolfScript, 30 bytes

:c-.{(c@-.{/))}{;1<}if}{;;1}if

The code in the header is purely to make the input easier - it just swaps around the order into another valid inpu format, just for ease of copying examples.

I thought this was going to be an 8-bte solution! And it is... kinda. Turns out we need two catches here, and each of them add a ton to make sure GS doesn't error out. Oh well. It works :)

Try it online!

\$\endgroup\$
1
\$\begingroup\$

PowerShell, 50 bytes

for($g,$c,$f=$args;($p+=$c)-lt$g;++$d){$p-=$f}1+$d

Try it online!

\$\endgroup\$
1
\$\begingroup\$

PowerShell, 60 59 bytes

-1 byte thanks to mazzy

param($g,$c,$f)for(){++$d;if(($p+=$c)-ge$g){$d;exit}$p-=$f}

Try it online!

An optimization of root's answer. If you like this, upvote his.

This runs forever on tests that are impossible. It also exits the script altogether on return so the test script is a bit sillier than normal.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ ? \$\endgroup\$
    – mazzy
    Mar 12, 2020 at 19:18
1
\$\begingroup\$

Python 3, 36 bytes

f=lambda h,a,b:h and-~f(h>>a<<b,a,b)

Try it online!

I/O format is questionable, but I think the idea is interesting enough for it to be posted.

Input:

  • h: If the depth of the well is d, h is an integer whose binary representation has d digits (ignoring leading zeros). For example, if d = 4, h can be 0b1111.
  • a: The climb distance
  • b: The fall distance, (positive integer).

Output: Number of days for the snail to climb out of the well. If it's impossible to climb out, runs forever.

Explanation

h>>a<<b will have the number of binary digits equals to the remaining height after one day. If the snail reaches the top during the day, then h>>a is 0, thus h>>a<<b is still 0.

The function is a recursive function that returns 0 if h==0, else recurs on h = h>>a<<b. The golfed code use the fact that -~x == 1+x to save 1 space.

\$\endgroup\$
0
\$\begingroup\$

R, 50 bytes

f=function(x,y,z)`if`(y>z,ceiling((x-z)/(y-z)),0)

Similar logic to others, let down by ceiling and function being such long words..

Usage:

> f(30,3,2)
[1] 28

> f(30,3,20)
[1] 0
\$\endgroup\$
1
  • \$\begingroup\$ f(5, 5, 10) should be 1. \$\endgroup\$
    – Scarabee
    Jul 8, 2017 at 22:37
0
\$\begingroup\$

Clojure - 80 70 bytes

(defn snail[x y z](cond(>= y x)1(> y z)(Math/ceil(/(- x z)(- y z))):else false))

Ungolfed version:

(defn snail [x y z]
  (cond
    (>= y x) 1
    (> y z)  (Math/ceil (/ (- x z) (- y z)))
    :else    false))

As a (shorter) anonymous function:

#(cond(<= %2 %1)1(> %2 %3)(Math/ceil(/(- %1 %3)(- %2 %3))):else false)

My original thinking (without checking, naturally) was that the % chars in the substitution markers would outweigh the benefit of getting rid of defn snail [x y z]. Apparently NOT! :-)

\$\endgroup\$
2
  • \$\begingroup\$ This is great! Since the function is not recursive, can you define it as an anonymous function instead to save bytes? \$\endgroup\$ Jul 9, 2017 at 22:28
  • \$\begingroup\$ I'd originally glanced at it and thought that the % chars on all the parameter references would outweigh the benefit of getting rid of defn snail [x y z]. Thanks for prodding me to reconsider this. :-) \$\endgroup\$ Jul 10, 2017 at 3:07
0
\$\begingroup\$

Add++, 31 bytes

D,f,@@@,¿@>,@$_+abRbUp${f}1+,1¿

Try it online!

First ever use of the ternary expression! Raises an Exception on invalid inputs

How it works

D,f,@@@,		; Declare a triadic function
			; Example arguments: 		[30 3 2]. Labelled [A B C] here
	¿		; If...
		@>	; 	A > B
	,		; Then...
		@	; 	Reverse;	STACK = [2 3 30]
		$_	; 	Swap subtract;	STACK = [2 27]
		+	; 	Add;		STACK = [29]
		abRbU	; 	Reversed args;	STACK = [29 2 3 30]
		p$	; 	Pop and swap;	STACK = [29 3 2]
		{f}	; 	Call 'f';	STACK = [27]
		1+	; 	Add 1;		STACK = [28]
	,		; Else...
	1		; 	Push 1;
	¿		; Endif
			;			Returns  28

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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