Skip to content

Commit

Permalink
Tweak and fix solution. 832 place
Browse files Browse the repository at this point in the history
  • Loading branch information
LovorDev committed Sep 22, 2023
1 parent b5194bf commit 6b39420
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 48 deletions.
119 changes: 83 additions & 36 deletions Mars Lander/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
*/
class Player
{
private const int TimeHorizontalLimit = 35;
private const int DistanceStopLimit = 1000;
private const int MinVelocityToFall = -40;
private const int RotationTweak = 500;

private static float _rotationMp = -1;
private static Vector2 _highestOnDirection;

Expand Down Expand Up @@ -43,31 +48,41 @@ private static void Main(string[] args)
var R = int.Parse(inputs[5]); // the rotation angle in degrees (-90 to 90).
var P = int.Parse(inputs[6]); // the thrust power (0 to 4).

UpdateRotationMultiplayer(flatSurface, position, marsLandPoints);

if (_rotationMp == -1)
{
var direction = Math.Sign((flatSurface.Center - position).X);
_highestOnDirection = marsLandPoints.Where(x => position.X - x.X > direction)
.OrderByDescending(x => x.Y).First();

_rotationMp = Math.Abs(_highestOnDirection.Y - position.Y) / 1000;
Console.Error.WriteLine($"RotationMP = {_rotationMp}");
}

SetPositionAboveLand(flatSurface, position, speed, marsLandPoints);
SetPositionAboveLand(flatSurface, position, speed);
}
}

private static void SetPositionAboveLand(FlatSurface flatSurface, Vector2 position, Vector2 speed,
private static void UpdateRotationMultiplayer(FlatSurface flatSurface, Vector2 position,
List<Vector2> marsLandPoints)
{
var allPicks = marsLandPoints.Where(x => x.X > position.X && x.X < flatSurface.Center.X ||
x.X < position.X && x.X > flatSurface.Center.X)
.OrderByDescending(x => x.Y).ToList();
if (!allPicks.Any())
{
return;
}

_highestOnDirection = allPicks.First();
_rotationMp = Math.Abs(_highestOnDirection.Y - position.Y) / RotationTweak;
Console.Error.WriteLine($"RotationMP = {_rotationMp}");
}

private static void SetPositionAboveLand(FlatSurface flatSurface, Vector2 position, Vector2 speed)
{
var direction = Math.Sign((flatSurface.Center - position).X);

var rotation = (int)(-10 * direction * _rotationMp);
var closestFlat = flatSurface.Center;

var trust = 4;
var absHorizontalSpeed = Math.Abs(speed.X);
var horizontalDistanceToFlat = Math.Abs(closestFlat.X - position.X);
var timeToFlatHorizontal = horizontalDistanceToFlat / absHorizontalSpeed;

if (Math.Abs(speed.X) < 30)
var trust = 4;
if (absHorizontalSpeed < 30)
{
trust = 4;
}
Expand All @@ -77,44 +92,36 @@ private static void SetPositionAboveLand(FlatSurface flatSurface, Vector2 positi
rotation = 0;
}

var closestFlat = flatSurface.Center;

if (Math.Abs(closestFlat.X - position.X) < 4000)
if (timeToFlatHorizontal < TimeHorizontalLimit)
{
if (Math.Abs(speed.X) > 50)
{
StopHorizontal(speed, ref rotation, ref trust);
Console.Error.WriteLine("Stop Horizontal by speed");
}
StopHorizontal(speed, ref rotation, ref trust);
Console.Error.WriteLine($"Stop Horizontal by speed. Time = {timeToFlatHorizontal}");
}

if (Math.Abs(closestFlat.X - position.X) < 1000)
if (horizontalDistanceToFlat < DistanceStopLimit)
{
if (Math.Abs(speed.X) > 20)
if (absHorizontalSpeed > 20)
{
StopHorizontal(speed, ref rotation, ref trust);
Console.Error.WriteLine(
$"Stop Distance={Math.Abs(closestFlat.X - position.X)} % = {(position.X - flatSurface.Center.X) / flatSurface.Length} R = {(position.X - flatSurface.Center.X) / flatSurface.Length * -50}");
$"Stop Distance={horizontalDistanceToFlat} % = {(position.X - flatSurface.Center.X) / flatSurface.Length} R = {(position.X - flatSurface.Center.X) / flatSurface.Length * -50}");
}
else
{
rotation = 0;
trust = 2;
trust = 3;
}
}

_highestOnDirection =
marsLandPoints.Where(x => position.X - x.X > direction).OrderByDescending(x => x.Y).First();

if (position.X < flatSurface.Left.X || position.X > flatSurface.Right.X)
{
var distToHeight = Math.Abs(_highestOnDirection.X - position.X);

var timeToHill = distToHeight / Math.Abs(speed.X);
var timeToHill = distToHeight / absHorizontalSpeed;

Console.Error.WriteLine($"Highest Diff {_highestOnDirection.Y - position.Y}. Time To: {timeToHill}");

if (_highestOnDirection.Y - position.Y > -300 && Math.Abs(speed.X) > 20)
if (_highestOnDirection.Y - position.Y > -300 && absHorizontalSpeed > 20)
{
StopHorizontal(speed, ref rotation, ref trust);
}
Expand All @@ -125,7 +132,7 @@ private static void SetPositionAboveLand(FlatSurface flatSurface, Vector2 positi
rotation /= 2;
}

if (speed.Y < -30)
if (speed.Y < MinVelocityToFall)
{
trust = 4;
}
Expand All @@ -135,10 +142,12 @@ private static void SetPositionAboveLand(FlatSurface flatSurface, Vector2 positi
trust = 2;
}

if (position.X > flatSurface.Left.X && position.X < flatSurface.Right.X)
var aboveFlat = position.X > flatSurface.Left.X && position.X < flatSurface.Right.X;

if (aboveFlat)
{
Console.Error.WriteLine(
$"Above flat. dist={position.X - flatSurface.Center.X}. % = {(position.X - flatSurface.Center.X) / flatSurface.Length}");

var distanceToSurface = position.Y - flatSurface.Center.Y;

if (direction == 1)
{
Expand All @@ -156,16 +165,54 @@ private static void SetPositionAboveLand(FlatSurface flatSurface, Vector2 positi
}
}

if (position.Y - flatSurface.Center.Y < 100)



const float g = -3.711f;
var landingVelo = CalculateFinalVelocity(speed.Y, g + trust, distanceToSurface);
trust = landingVelo switch
{
> 40 => 4,
< 40 and > 30 => 3,
< 30 and > 20 => 2,
< 20 and > 10 => 1,
< 10 => 0,
_ => trust
};

if (distanceToSurface < 120)
{
rotation = 0;
trust = landingVelo switch
{
> 40-4 => 4,
< 40-4 and > 30-3 => 3,
< 30-3 and > 20-2 => 2,
< 20-2 and > 10-1 => 1,
< 10-1 => 0,
_ => trust
};
}

Console.Error.WriteLine(
$"Above flat. dist=x:{position.X - flatSurface.Center.X} y:{distanceToSurface}. % = {(position.X - flatSurface.Center.X) / flatSurface.Length}");
}


rotation = Math.Clamp(rotation, -90, 90);
trust = Math.Clamp(trust, 0, 4);

Console.WriteLine($"{rotation} {trust}");
}

public static float CalculateFinalVelocity(float initialVelocity, double g, float s)
{
var poweredCalculation = Math.Pow(initialVelocity, 2) + 2 * g * s;
var velocityOnLand = (float)Math.Sqrt(poweredCalculation);
Console.Error.WriteLine($"velocityOnLand: {velocityOnLand}");
return velocityOnLand;
}

private static void StopHorizontal(Vector2 speed, ref int rotation, ref int trust)
{
var clamp = 20 * _rotationMp;
Expand Down
14 changes: 2 additions & 12 deletions Mars Lander/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
<h2>This my solution for puzzle [Mars Lander](https://www.codingame.com/multiplayer/optimization/mars-lander) </h2>

<h3>Place in Leaderboard [ 832 ]</h3>
The goal for program is to safely land the "Mars Lander" shuttle, the landing ship which contains the Opportunity rover. Mars Lander is guided by a program, and right now the failure rate for landing on the NASA simulator is unacceptable.






<img alt="" src="https://www.codingame.com/fileservlet?id=2635325710601" style="width: 100%;background-color: #eeeeee;">

<iframe width="100%" frameborder="0" scrolling="no" allowtransparency="true" style="visibility:hidden" src="https://www.codingame.com/playground-widget/6765e1a734d26d78601c5e1cfef5ff7350735/the-codingame-api/1103695"></iframe>
<script>if(void 0===window.techioScriptInjected){window.techioScriptInjected=!0;var d=document,s=d.createElement("script");s.src="https://files.codingame.com/codingame/iframe-v-1-4.js",(d.head||d.body).appendChild(s)}</script>

<img alt="" src="https://www.codingame.com/fileservlet?id=2635325710601" style="width: 100%;background-color: #eeeeee;">

0 comments on commit 6b39420

Please sign in to comment.