Predict the Bomb's Landing Point!

1

1

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!

pydude

Posted 2017-04-04T21:10:27.023

Reputation: 777

6Once you do out the math, it looks like the formula leaves little to golf. – xnor – 2017-04-04T21:23:38.373

@xnor Maybe the ceiling function could be golfed. But I can't see anywhere else. – fəˈnɛtɪk – 2017-04-04T21:24:30.583

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? – James – 2017-04-04T21:26:09.380

Looks like 0 is valid input from test case #1 – Timtech – 2017-04-04T21:27:02.707

0 is a valid input only for start-x and start-y. editing now. – pydude – 2017-04-04T21:28:09.523

Since this is such a trivial question, please do not upvote answers in obvious or golfing languages.

– devRicher – 2017-04-05T09:50:52.837

"Normal day", "military base" being in your "military base" isnt "normal". also, If you make a military base you might as well make it an explosion-proof bunker. – Matthew Roh – 2017-04-05T09:59:14.050

Answers

1

05AB1E, 9 bytes

¹³²(I÷(*+

Try online

P. Knops

Posted 2017-04-04T21:10:27.023

Reputation: 301

1

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

fəˈnɛtɪk

Posted 2017-04-04T21:10:27.023

Reputation: 4 166

1You could do (a,b,x,y)=>a+x*(b/y|0+1) and save 5 bytes – powelles – 2017-04-04T22:28:57.550

1@powelles If I do that I will end up overshooting for exact values. – fəˈnɛtɪk – 2017-04-04T23:54:54.793

Weird. I expected x|0+1 to perform the same as Math.ceil and didn't actually test it. – powelles – 2017-04-05T00:30:50.820

@powelles if you have 1|0+1 you end up with 2. With Math.ceil you will get 1 – fəˈnɛtɪk – 2017-04-05T00:36:08.923

I mistakenly made the assumption that ceiling could be implemented as floor+1. – powelles – 2017-04-05T00:53:45.147

There is a way to emulate ceiling with integer math, but in this case it saves only 1 byte: (a,b,x,y)=>a+x*((b+y-1)/y|0) – user5090812 – 2017-04-10T17:48:12.340

1

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

Timtech

Posted 2017-04-04T21:10:27.023

Reputation: 12 038

Don't you need to prompt for x and y as well? – fəˈnɛtɪk – 2017-04-04T21:28:16.107

@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. – Timtech – 2017-04-04T22:03:59.507

1

Python, 45 28 Bytes

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

Try it online!

Anthony Pham

Posted 2017-04-04T21:10:27.023

Reputation: 1 911

The import has to be scored. meta post

– mbomb007 – 2017-04-04T21:28:10.937

@mbomb007 Still, with the import is 45 bytes – Anthony Pham – 2017-04-04T21:28:30.073

Why not use your own ceil function? lambda a,b,x,y:a+x*int(b/y+.5) – James – 2017-04-04T21:32:29.560

@DJMcMayhem Just made one 2 bytes shorter than yours – Anthony Pham – 2017-04-04T21:48:40.313

a--b//y*x seems to work. – Ørjan Johansen – 2017-04-05T01:25:08.183

If you use Python 2, I think you can eliminate the // and just put /. – HyperNeutrino – 2017-04-05T01:55:49.780

Combining ØrjanJohansen's suggestions and my suggestion: lambda a,b,x,y:a--b/y*x in Python 2 for 23 bytes. – HyperNeutrino – 2017-04-05T02:01:21.280

@DJMcMayhem wouldn't that do a round? a ceil is (b+y-1)/y – Felipe Nardi Batista – 2017-04-05T14:36:53.047

You could use a-x*(...) to save 1 byte – G B – 2017-04-10T08:00:43.310

1

Scheme 40 36 Bytes

λ(x y u v)(+ x(* u(ceiling(/ y v))))

Penguino

Posted 2017-04-04T21:10:27.023

Reputation: 231

Does scheme require all of those spaces between identifiers and parentheses – Generic Display Name – 2017-04-04T21:57:29.383

Actually I can get rid of some of them...thanks – Penguino – 2017-04-04T22:00:31.600

1

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 :)

Generic Display Name

Posted 2017-04-04T21:10:27.023

Reputation: 365

Ouch, importing Data.Ratio ... try a-x*div(-b)y. – Ørjan Johansen – 2017-04-05T01:28:23.033

0

C#, 46 bytes

(a,b,c,d)=>a+c*(int)Math.Ceiling((double)b/d);

CHENGLIANG YE

Posted 2017-04-04T21:10:27.023

Reputation: 37

0

Ruby, 21 bytes

->x,y,v,w{x-v*(y/-w)}

G B

Posted 2017-04-04T21:10:27.023

Reputation: 11 099

0

Java 7, 66 bytes

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

adrianmp

Posted 2017-04-04T21:10:27.023

Reputation: 1 592