Let's start:
On codinggame you have this problem to result :
you know the g, then land, the initial position of spaceship and you can control it's rotation and power to land on flat terrain with constraints of fuel, rotation and speeds (vertical and horizontal).
Math solution ?
First when I saw this problem, I write all the constraints, all the variables and all the processes in formulas, it's looks like a mess...... you can never resolve these equations because you have too much possibilities....
Think it simply:
So then I tried to recall the Mars Lander 1 : you don't have to move on horizontal axis, just lander with a mars'gravity and control the power of the spaceship to make it landed. What you will do ?
First thing I was thinking it to calculate ...... when I will get a maximum speed and I need to put my max power to slow down etc.... But why not think simple: when max speed is passed, I incremental my power out, when it's not passed, I power out less.
Strategy:
So infect, the right think to do it to ignore most of the constraints and think about the objective : landing. It's like a unit test, first thing to to is landing and don't think about fuel , path, best way, etc.
Just to land.
A problem could be complex, you need to start with a simple strategy, then with different test case, you could make it better to cover the use cases. Never think about to make a perfect algorithm in the beginning to make everything works.
So, thinks simply and want could we do the smallest change to make it landed :
you have already a strategy to landing without horizontal move (CODE1), so for the second problem is just write a CODE2 to move on a plat terrain then call CODE1.
And CODE2 can be written in two part : CODE2.1 fiind a plat terrain [x1, x2] then CODE2.2 move to [x1, x2].
CODE2.1 is easy.
CODE2.2 what should we do when we move? the answer is to keeping your height y. SO you could check you vertical speed to limit it in [-minVspeed, 0] (you can drop a little down but never too much), and you have to have a horizon speed to move to your destination moveHspeed (not too big because you will need to slow down when you close your destination's x). Then we can see a constraint comes : you have a power and you have rotation, when you get more V acceleration, you got less H acceleration. So you could do is adjust the minVspeed and moveHspeed , to get a moveHspeed, then let the rotation 0 to not change the moveHspeed then just adjust power to keep the height, later you need to do the same thing to slow down to 0 (start slow down when you are right in [x1, x2]), then when you slow Hspeed to 0, you can call CODE1 to land.
I know we can do much better then that, but you will see, with this algo, we can already get a good note.
Can we do better ?
always ask this question. With a limit fuel, and different initial status of you spaceship, you need to add more strategy to you CODE2.2
CODE2.2.1 : to save more fuel, you can start to slow down you hspeed with a distance before enter in [x1, x2]
CODE2.2.2 : check you initial status, correct it as soon as possible (ex: you got a big initial Hspeed, you need to slow down to your wanted moveHspeed but the same time you cannot drop too fast neither go up, nor get too far on X axis out of game scale). this need you to adjust your wanted Hspeed and mxinVspeed to drop down to make it work.
.......
In most case, your code work is not because your algo is perfect, it's because the test case matches well you adjustment argument :(minVspeed, moveHspeed) and you strategy.
Ok, as a perfectionist, what can be improved : too much! but you need more test case to discover them and adjust your strategy and parameters.
So, TDD! I don't need a perfect algo, it's enough to make all your test case work.
No comments:
Post a Comment