2
\$\begingroup\$

Introduction

It was just a normal day, here at your military base. Suddenly, you looked up to see a bomb falling down from the sky! You need to create a program to predict where it will fall, and tell you the location.

Challenge

You will be given a bomb in the form of Start-X Start-Y X-velocity Y-velocity. You can assume that they will all be positive integers. 0 is a valid input only for Start-x and Start-y. The bomb starts with x Start-x, and y Start-y. Each "frame" (no graphical output required), the bomb will move x-velocity units right, then y-velocity units down. Continue doing this until the bomb reaches y of 0 or below. Then, output what it's x was when it hit the ground.

Test Cases

1

Input:0 10 2 2

Output:10

2

Input:5 4 3 2

Output:11

3

Input:11,8,7,3

Output:32

This is , so shortest answer in bytes wins!

\$\endgroup\$
7
  • 6
    \$\begingroup\$ Once you do out the math, it looks like the formula leaves little to golf. \$\endgroup\$
    – xnor
    Commented Apr 4, 2017 at 21:23
  • \$\begingroup\$ @xnor Maybe the ceiling function could be golfed. But I can't see anywhere else. \$\endgroup\$ Commented Apr 4, 2017 at 21:24
  • \$\begingroup\$ You can assume that they will all be positive integers 0 is not a positive integer. Can you clarify if 0 is a valid input or not? \$\endgroup\$
    – DJMcMayhem
    Commented Apr 4, 2017 at 21:26
  • \$\begingroup\$ Looks like 0 is valid input from test case #1 \$\endgroup\$
    – Timtech
    Commented Apr 4, 2017 at 21:27
  • \$\begingroup\$ 0 is a valid input only for start-x and start-y. editing now. \$\endgroup\$
    – Luke B
    Commented Apr 4, 2017 at 21:28

9 Answers 9

1
\$\begingroup\$

05AB1E, 9 bytes

¹³²(I÷(*+

Try online

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

TI-Basic, 21 bytes

Input :Prompt A,B:X+A+Aint(Y/B-E~9

If the ceiling function did not have to be implemented, it would be more straightforward at 13 bytes:

Input :Prompt A,B:X+AY/B
\$\endgroup\$
2
  • \$\begingroup\$ Don't you need to prompt for x and y as well? \$\endgroup\$ Commented Apr 4, 2017 at 21:28
  • \$\begingroup\$ @fəˈnɛtɪk In TI-83/84 BASIC, providing no arguments to Input is equivalent to Prompt X,Y, taking location from the graph screen. It's usually more of a savings when only one or two values are needed, although it saves 1 byte in this situation as well. \$\endgroup\$
    – Timtech
    Commented Apr 4, 2017 at 22:03
1
\$\begingroup\$

Python, 45 28 Bytes

lambda a,b,x,y:a+x*-(-b//y)

Try it online!

\$\endgroup\$
9
  • \$\begingroup\$ The import has to be scored. meta post \$\endgroup\$
    – mbomb007
    Commented Apr 4, 2017 at 21:28
  • \$\begingroup\$ @mbomb007 Still, with the import is 45 bytes \$\endgroup\$ Commented Apr 4, 2017 at 21:28
  • \$\begingroup\$ Why not use your own ceil function? lambda a,b,x,y:a+x*int(b/y+.5) \$\endgroup\$
    – DJMcMayhem
    Commented Apr 4, 2017 at 21:32
  • \$\begingroup\$ @DJMcMayhem Just made one 2 bytes shorter than yours \$\endgroup\$ Commented Apr 4, 2017 at 21:48
  • \$\begingroup\$ a--b//y*x seems to work. \$\endgroup\$ Commented Apr 5, 2017 at 1:25
1
\$\begingroup\$

Scheme 40 36 Bytes

λ(x y u v)(+ x(* u(ceiling(/ y v))))
\$\endgroup\$
2
  • \$\begingroup\$ Does scheme require all of those spaces between identifiers and parentheses \$\endgroup\$ Commented Apr 4, 2017 at 21:57
  • \$\begingroup\$ Actually I can get rid of some of them...thanks \$\endgroup\$
    – Penguino
    Commented Apr 4, 2017 at 22:00
1
\$\begingroup\$

Haskell, 21 bytes

(a#b)x y=a-x*div(-b)y

The trivial answer: defines an operator that takes 4 arguments and then uses the formula.

Could probably be golfed more.

EDIT: Yep, Ørjan Johansen points out we can use a-x*div b(-y), which brings us to 22 bytes. Try it online!

At this point, the code is really more other people than mine, so I'm going to mark this answer community wiki. Feel free to golf this further if you want :)

\$\endgroup\$
1
  • \$\begingroup\$ Ouch, importing Data.Ratio ... try a-x*div(-b)y. \$\endgroup\$ Commented Apr 5, 2017 at 1:28
1
\$\begingroup\$

JavaScript, 29 28 bytes

Thanks @user5090812 for golfing off 1 byte

(a,b,x,y)=>a+x*((b+y-1)/y|0)

Try it online!

If it did not have to move x then y but was instead falling in a straight line it would be 18 bytes

(a,b,x,y)=>a+x*b/y
\$\endgroup\$
6
  • 1
    \$\begingroup\$ You could do (a,b,x,y)=>a+x*(b/y|0+1) and save 5 bytes \$\endgroup\$
    – powelles
    Commented Apr 4, 2017 at 22:28
  • 1
    \$\begingroup\$ @powelles If I do that I will end up overshooting for exact values. \$\endgroup\$ Commented Apr 4, 2017 at 23:54
  • \$\begingroup\$ Weird. I expected x|0+1 to perform the same as Math.ceil and didn't actually test it. \$\endgroup\$
    – powelles
    Commented Apr 5, 2017 at 0:30
  • \$\begingroup\$ @powelles if you have 1|0+1 you end up with 2. With Math.ceil you will get 1 \$\endgroup\$ Commented Apr 5, 2017 at 0:36
  • \$\begingroup\$ I mistakenly made the assumption that ceiling could be implemented as floor+1. \$\endgroup\$
    – powelles
    Commented Apr 5, 2017 at 0:53
0
\$\begingroup\$

C#, 46 bytes

(a,b,c,d)=>a+c*(int)Math.Ceiling((double)b/d);
\$\endgroup\$
0
\$\begingroup\$

Ruby, 21 bytes

->x,y,v,w{x-v*(y/-w)}
\$\endgroup\$
0
\$\begingroup\$

Java 7, 66 bytes

int f(int a,int b,int x,int y){return a+x*(int)Math.ceil(1f*b/y);}
\$\endgroup\$

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.