47
\$\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\$

39 Answers 39

1
\$\begingroup\$

Jelly, 10 bytes

ẋ;\m2S€<i0

Try it online!

\$\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\$
  • \$\begingroup\$ Excel solution does not seem to handle cases where climb distance = well depth. (5,5,10) (7,7,7) \$\endgroup\$ – Wernisch Jul 10 '17 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\$ – Taylor Scott Jul 10 '17 at 12:18
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\$
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\$
  • \$\begingroup\$ f(5, 5, 10) should be 1. \$\endgroup\$ – Scarabee Jul 8 '17 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\$
  • \$\begingroup\$ This is great! Since the function is not recursive, can you define it as an anonymous function instead to save bytes? \$\endgroup\$ – musicman523 Jul 9 '17 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\$ – Bob Jarvis Jul 10 '17 at 3:07
0
\$\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\$
0
\$\begingroup\$

bash, 39 bytes

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

Try it online!

\$\endgroup\$
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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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