2
\$\begingroup\$

There is a helicopter flying 500 meters from the ground. It can fly up 100 meters, which takes 1.0 secs. Or if it doesn't fly up it will fall 200 meters, which also takes 1.0 secs - This means if the instruction is anything other than "Fly" the helicopter falls. Build a program that takes input of a list of instructions, telling the helicopter when to fly and when not to. When the helicopter falls it should output the time counter.

INPUT:

Fly Dance Fly Golf Fly Fly

OUTPUT:

8.5 secs

Walkthrough:

1) Fly == Fly so +100 = 600m

2) Dance != Fly so -200 = 400m

3) Fly == Fly so +100 = 500m

4) Golf != Fly so -200m = 300m

5) Fly == Fly so +100 = 400m

6) Fly == Fly so +100 = 500m

7) NO INSTRUCTIONS so -200 = 300m

8) NO INSTRUCTIONS so -200 = 100m

9) NO INSTRUCTIONS so -100 = 0m (This takes 0.5 seconds)

Each instruction should be separated by a space, and if the instuction isn't fly, then automatically fall. When the program runs out of instructions automatically fall. Your program must output in (float)secs, and if it takes => 60.0 seconds display in mins and seconds eg. 1 mins 32.0 secs

Seconds are float decimal values, minutes are whole interger values. Also note capitalization does not matter your program should work for capitals or no capitals.

This is code-golf so the least NO bytes wins!

\$\endgroup\$
  • 3
    \$\begingroup\$ Could you clarify this? Perhaps a step by step walk through of the example. Is the word "fall" being used both for dropping 200 meters, and for reaching 0 meters? Perhaps using a different word for reaching 0 meters would make this clearer. Is the output the time when the helicopter reaches 0 meters? \$\endgroup\$ – trichoplax Nov 15 '16 at 18:47
  • 2
    \$\begingroup\$ Can we assume that if the helicopter is at 100m, and falls, it still takes 1 second to reach 0m, even though it usually takes 1 second to drop by 200m? \$\endgroup\$ – trichoplax Nov 15 '16 at 18:57
  • 4
    \$\begingroup\$ I think this could be a good question is it is asked more clearly maybe with some examples. May I suggest the Sandbox to get some feedback prior to posting next time? \$\endgroup\$ – ElPedro Nov 15 '16 at 18:59
  • 1
    \$\begingroup\$ if it dosen't fly up, it will fall 200m does that mean heli can crash and after crashing it should not fly anymore which means rest of the instructions should be ignored? Example : please tell me output if the input is this : fly fall fall fall fall fly fly fly fly \$\endgroup\$ – Mukul Kumar Nov 15 '16 at 19:26
  • 1
    \$\begingroup\$ The string "M min S secs" specifically, or are normal output formats like a list of [M,S] and other reasonable formats like "M:S" OK? If specifically, then can less than one minute output "0 min S secs"? \$\endgroup\$ – Jonathan Allan Nov 15 '16 at 19:53
2
\$\begingroup\$

Perl, 90 bytes

$a=5;
$_++,$a+=shift=~/Fly/?1:-2 while$a;
$==$_/60;
$_%=60;
say+($=?"$= mins ":"")."$_ secs"

Accepts input as command-line parameters. Run as

perl -E '$a=5;$_++,$a+=shift=~/Fly/?1:-2 while$a>0;$==$_/60;$_%=60;say+($=?"$= mins ":" ")."$_ secs"' Fly Dance Fly Fly Golf

Starts $a at 5, and increases it by 1 every time it finds "Fly", decrements by 2 otherwise. Keeps going until $a is non-positive, and counts how many iterations it took to get there. $= is special and forces its value to be an integer, so $==$_/60 only takes the integer portion of the division, ignoring the rest. Everything else is self-explanatory, I feel.

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

C (with lots of warnings) 108

i=1,h=5;main(c,v)char**v;{for(;h>0;i++)h+=1-3*!!strcmp(v[i<c?i:0],"Fly");printf("%f secs\n",i-(h<0?1.5:1));}
\$\endgroup\$
0
\$\begingroup\$

Scala, 88 bytes

s=>{val n=s.split(" ").count("Fly"==)*1.5+2.5
(if(n<60)""else n/60+"min ")+n%60+" secs"}

Ungolfed:

s=>{
  val n=s.split(" ").count("Fly"==)*1.5+2.5
  (
    if(n<60)
      ""
    else
      (n/60)+"min "
  )
  +(n%60)
  +" secs"
}

Uses the same formula as Copper's python answer.

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

PHP, 55 54 bytes

for($h=5;$h>0;)$h+='Fly'==$argv[++$i]?:-2;echo$h/2+$i;

Loops through command line arguments; run with -r.

Height is divided by 100 to simplify output and calculation: If height is below zero (i.e. -100m),
adding half of it (-50 divided by 100: -.5) to the time removes half a second from the result.

That combined with the magic of Elvis golfed 5 6 bytes from my initial program.

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

Python 2, 85 91 bytes

t=2.5+input().upper().split().count('FLY')*1.5
print('%d mins'%(t/60))*(t>59),`t%60`,'secs'

Takes input on STDIN, surrounded by quotes (single or double). Simply counts the number of Flys there are in the input, performs some path to calculate the number of seconds, then formats it.

Now case-insensitive!

Fixed a bug with the same bytecount thanks to Jonathan Allan!

Saved 2 bytes thanks to Shebang!

\$\endgroup\$
  • \$\begingroup\$ Bit of a nitpick, but "Marty McFly went to the future" would print 4.0 secs rather than 2.5 secs. Fixable for one byte with t=(5+input().split().count('Fly')*3)/2. \$\endgroup\$ – Jonathan Allan Nov 17 '16 at 6:26
  • 1
    \$\begingroup\$ ...but you can golf that cost off with t=2.5+input().split().count('Fly')*1.5. \$\endgroup\$ – Jonathan Allan Nov 17 '16 at 6:29
  • 1
    \$\begingroup\$ Since I don't think leading spaces matter, this print statement should work: print('%d mins'%(t/60))*(t>59),``t%60``,'secs' (replace double ticks with single ticks). It only has a leading space if t < 60. \$\endgroup\$ – Kade Nov 18 '16 at 17:26
  • \$\begingroup\$ Also this would return a float for the minutes if it's a float above 60, i.e. 80.5 secs -> 1.3416666 minutes. \$\endgroup\$ – Kade Nov 18 '16 at 17:44
  • \$\begingroup\$ @Shebang It doesn't return a float for the minutes. The %d implicitly casts to an int -- ideone.com/JKOS0H. Thanks for the tip though! \$\endgroup\$ – Copper Nov 18 '16 at 18:54
0
\$\begingroup\$

AWK, 150 bytes

Below is a rather straightforward implementation in AWK, the nice thing about it is that you can run it interactively and feed it by typing commands by hand (just don't forget to remove RS=" ", if you want one command per line).

Golfed

awk 'BEGIN{RS=" ";h=500;t=0}toupper($0)~/FLY/{h+=100;t+=1;next}{h-=200;t+=1;}END{t+=h/200;F=(t>59?"%d mins ":"%.0d") "%.1f secs";printf F,t/60,t%60;}'

Test

echo "Fly Dance FLy Golf Fly Fly" |\
awk 'BEGIN{RS=" ";h=500;t=0}toupper($0)~/FLY/{h+=100;t+=1;next}{h-=200;t+=1;}END{t+=h/200;F=(t>59?"%d mins ":"%.0d") "%.1f secs";printf F,t/60,t%60;}'

8.5 secs
\$\endgroup\$
0
\$\begingroup\$

Mathematica 211 bytes

(s=ToString;t=Length[#[[;;(FirstPosition[#,x_/;x<=0,1]-1/. 0->Length@#)]]]&[x=500+Accumulate[If[#=="Fly",100,-200]&/@(StringSplit@#)]];
If[#>=60,s@Floor[#/60]~~" min ",""]~~s@Mod[#,60]~~ " sec"&[t+x[[t]]/200.])&

This algorithm assumes the flight ends when the altitude reaches 0 for the first time. Looking at some of the other answers, I think people have the helicopter flying below ground level. I don't allow "touch and go" either.

Ungolfed:

in="Fly Dance Fly Golf Fly Fly";
s=ToString;StringSplit@in
If[#=="Fly",100,-200]&/@%
altList=500+Accumulate@%
l=Length@altList
(* find last positive before first nonpositive *)
FirstPosition[altList,x_/;x<=0,1]-1
t=Length[altList[[;;(%/. 0->l)]]]
t+altList[[t]]/200.
If[%>=60,s@Floor[%/60]~~" min ",""]~~s@Mod[%,60]~~ " sec"
\$\endgroup\$
0
\$\begingroup\$

Python, 100 92 89 bytes

lambda I:sum(reduce(lambda(a,s),i:(a+(a>0)*(-1,.5)[i=='Fly'],s+(a>0)),I.split(),(2.5,0)))

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.