# [Community Puzzle] Asteroids

https://www.codingame.com/training/easy/asteroids

Created by @XorZy,validated by @codybumba,@AgathokakologicalBit and @tuna_in_cream.
If you have any issues, feel free to ping them.

1 Like

Nice little puzzle.
I lazily casted int instead of actually using the floor function to round down and it didnâ€™t work at first cause it would round -0.5 to 0 instead of -1.
So donâ€™t be lazy and use the proper floor function

8 Likes

Lots of fun!

2 Likes

The puzzle implicitly assumes that the visible path of an asteroid is a straight line, which holds only if t1, t2, t3 are close. Of course, fair enough for an easy puzzle.

It would be a very tough math/physics (not coding) problem if we would need to take into account gravity, Sun, Earth rotation, pictures taken on different days, etc.

1 Like

If you go that route, youâ€™d need to know general relativity for space time curvature

3 Likes

I used `int` with Python and it worked.

2 Likes

Interesting. int(-0.5) should definitely return 0 instead of -1, thus causing the asteroid â€śVâ€ť to appear in the final picture when it should be out of screen.
Perhaps the reason it worked is because you are filtering out negative coordinates before the conversion?

1 Like

Keep it in mind for Asteroids Ep. 2

1 Like

For JVM users, there is a not widely known method, Math.floorDiv(), fits well for this purpose.

2 Likes

Yes I am and it wasnâ€™t on purpose.

I donâ€™t understand that part of statement:
If two or more asteroids have the same final coordinates, output only the closest one .

The closest one is determined how if the coordinates are the same? If I have A(0,1) B(0,1) C(0,1) which one is the closest one and why?

1 Like

A is closer than B, that is closer than C, and so on.
Itâ€™s in the statement.

True, I donâ€™t know why but I couldnâ€™t see it.
Thank you

1 Like

OK what is happening with â€śGreater deltaâ€ť??? The A clearly moves 4 spots, then it suddenly moves 1 spot? This is extremely confusing.
OH time is a part of it. ok
So after 5 mins including time differences, I figured it out.

8 Likes

Yeah it wouldnâ€™t make sense to provide three different times as input if youâ€™re not supposed to use them

1 Like

I wouldnâ€™t disagree, and to be honest I rushed this one due to it being late. But that being said, many many puzzles have unused information. Or at least information gathered from other means work. But youâ€™re not wrong.

1 Like

Hi, I passed all the test and all validators except the last â€śValidator 10â€ťâ€¦ I didnt hard code any thing to pass the last test case, Not able to debug too â€¦ can anyone help me? Coding language: Python

If you round down with int, try with math.floor instead.

1 Like

Itâ€™s hard to tell what you did wrong without seeing your code but here are a few things to consider :

• Use floats and not integers to compute the coordinates

• Use floor on the final result, not int() as it will cause rounding issues

• Make sure you discard asteroids that are outside the picture (x<0,y<0,x>=W,y>=H)

• Make sure you only output the closest asteroid in case more than one have the same coordinates (A being the closest)

1 Like

I used `int` and my solution passed all the validators.