From 6b394209243f3cc50f9d69a712c4f4b138cd0b57 Mon Sep 17 00:00:00 2001 From: Coolinar Date: Fri, 22 Sep 2023 20:49:21 +0500 Subject: [PATCH] Tweak and fix solution. 832 place --- Mars Lander/Program.cs | 119 ++++++++++++++++++++++++++++------------- Mars Lander/README.md | 14 +---- 2 files changed, 85 insertions(+), 48 deletions(-) diff --git a/Mars Lander/Program.cs b/Mars Lander/Program.cs index 5263a74..45e79be 100644 --- a/Mars Lander/Program.cs +++ b/Mars Lander/Program.cs @@ -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; @@ -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 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; } @@ -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); } @@ -125,7 +132,7 @@ private static void SetPositionAboveLand(FlatSurface flatSurface, Vector2 positi rotation /= 2; } - if (speed.Y < -30) + if (speed.Y < MinVelocityToFall) { trust = 4; } @@ -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) { @@ -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; diff --git a/Mars Lander/README.md b/Mars Lander/README.md index 17bfc8b..0404d5c 100644 --- a/Mars Lander/README.md +++ b/Mars Lander/README.md @@ -1,14 +1,4 @@

This my solution for puzzle [Mars Lander](https://www.codingame.com/multiplayer/optimization/mars-lander)

- +

Place in Leaderboard [ 832 ]

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. - - - - - - - - - - - + \ No newline at end of file