Advertisement
Guest User

Codingame Mars Lander 2 Simulation Test

a guest
Apr 26th, 2017
710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.38 KB | None | 0 0
  1. #include <bits/stdc++.h> //All main STD libraries
  2. #include <x86intrin.h> //SSE Extensions
  3. using namespace std;
  4.  
  5. #define TESTSIM
  6. //#define DUMP
  7. #define FOR0(i,n) for(int i=0;i<(n);++i)
  8.    
  9. const int DEPTH = 5;
  10.  
  11. const int ON_AIR = 0;
  12. const int LANDED = 1;
  13. const int LAND_BAD_ANGLE = 2;
  14. const int LAND_BAD_SPEED = 3;
  15. const int CRASH = 4;
  16.  
  17.  
  18. const double MAX_LAND_VSPEED = 40.0f;
  19. const double MAX_LAND_HSPEED = 20.0f;
  20. const double GRAVITY = -3.711;
  21. const double T = 1.0;
  22.  
  23.  
  24.  
  25. class Line {
  26.   public:
  27.     double x0,y0,x1,y1,s1_x,s1_y;
  28.     Line(){x0=0.0f;x1=0.0f;y0=0.0f;y1=0.0f;}
  29.     Line(double _x0,double _y0,double _x1,double _y1)
  30.     {
  31.       x0=_x0;y0=_y0;x1=_x1;y1=_y1;
  32.       s1_x = x1 - x0;
  33.       s1_y = y1 - y0;
  34.     }
  35.     bool collides(double landerx0,double landery0,double landerx1,double landery1){
  36.       double s2_x = landerx1 - landerx0;
  37.       double s2_y = landery1 - landery0;
  38.       double coef = 1 / (-s2_x * s1_y + s1_x * s2_y);
  39.       double s = (-s1_y * (x0 - landerx0) + s1_x * (y0 - landery0)) *coef;
  40.       double t = ( s2_x * (y0 - landery0) - s2_y * (x0 - landerx0)) *coef;
  41.       return (s >= 0 && s <= 1 && t >= 0 && t <= 1);      
  42.     }
  43.     bool isLandingZone(){
  44.         return s1_y == 0;
  45.     }
  46. };
  47. int surfaceN;
  48. Line LandingZone;
  49. vector<Line> landLines;
  50. ostream &operator<<(ostream& output, const Line& l){
  51.         output << "Line: ("<<l.x0<<","<<l.y0<<")->("<<l.x1<<","<<l.y1<<")  Dist:"<<l.s1_x<<","<<l.s1_y;
  52.         return output;
  53.     }
  54.  
  55.  
  56. class Genoma{
  57. public:
  58.     int rotation[DEPTH];
  59.     double power[DEPTH];
  60.     double Score;
  61. };
  62. ostream &operator<<(ostream& output, const Genoma& g){
  63. output << "int[] rotation = {";  
  64.    FOR0(i,DEPTH) output << (i>0?",":"")<<g.rotation[i];
  65. output << "};"<<endl;
  66. output << "int[] power = {";  
  67.    FOR0(i,DEPTH) output << (i>0?",":"")<<g.power[i];
  68. output << "}; //Score:"<<g.Score<<endl;
  69.         return output;
  70.     }
  71.    
  72.    
  73.    
  74. class GameState{
  75. public:
  76.   int turn;
  77.   double prev_x,prev_y,x,y,vx,vy,ax,ay,fuel,power;
  78.   int angle;
  79.   GameState(){turn = -1;ax=0;ay=0;angle=90;}
  80.  
  81.   void ReadConfig(istream& input){
  82.     input >> surfaceN; input.ignore();
  83.     #ifdef DUMP
  84.     cerr << surfaceN<<endl;
  85.     #endif
  86.     double oldX, oldY;
  87.     for (int i = 0; i < surfaceN; i++) {
  88.         double landX;
  89.         double landY;
  90.         input >> landX >> landY; input.ignore();
  91.         #ifdef DUMP
  92.          cerr << landX<<" "<<landY<<endl;
  93.         #endif
  94.         if(i>0){
  95.           Line newLine = Line(oldX,oldY, landX,landY);
  96.           if(newLine.isLandingZone()){
  97.             LandingZone = newLine;
  98.             #ifndef DUMP
  99.             cerr << i<< " LANDZONE "<<newLine<<endl;          
  100.             #endif
  101.           }
  102.            
  103.          
  104.           landLines.push_back(newLine);
  105.         }
  106.         oldX = landX;
  107.         oldY = landY;
  108.     }    
  109.     #ifndef DUMP
  110.      for (int i = 0; i < surfaceN-1; i++)
  111.       cerr <<(i+1)<<" :"<< landLines[i]<<endl;
  112.     #endif      
  113.      
  114.   }
  115.  
  116.   void ReadTurn(istream& input)
  117.   {
  118.       input >> x >> y >> vx >> vy >> fuel >> angle >> power; input.ignore();
  119.      
  120.       ++turn;
  121.      if (turn == 0)
  122.      {
  123.          prev_x = x;
  124.          prev_y = y;
  125.          #ifdef DUMP
  126.          cerr << x << " "<< y << " " << vx << " " <<vy << " "<< fuel << " "<< angle << " "<< power<<endl;
  127.          #endif
  128.      }
  129.      angle += 90;
  130.   }
  131.  
  132.   bool Equals(const GameState& gs);
  133.  
  134.   int isLanded()
  135.   {
  136.       int result = ON_AIR;
  137.      //Check Landing
  138.      if (x <0) return CRASH;
  139.      if (y <0) return CRASH;
  140.      if (y >=3000) return CRASH;
  141.      if (x >=7000) return CRASH;
  142.      if (LandingZone.collides(prev_x,prev_y,x,y))
  143.      {
  144.          if (angle != 90)
  145.          {
  146.       //       cerr << "Error: Bad Angle on Land "<<angle<<" != 90"<<endl;
  147.              return LAND_BAD_ANGLE;
  148.          }
  149.          if (abs(vy)>MAX_LAND_VSPEED)
  150.          {
  151.     //         cerr << "Error: Too Fast on Vspeed "<<vy<<" > "<<MAX_LAND_VSPEED<<endl;
  152.              return LAND_BAD_SPEED;
  153.          }
  154.          if (abs(vx)>MAX_LAND_HSPEED)
  155.          {
  156.   //           cerr << "Error: Too Fast on Hspeed "<<vy<<" > "<<MAX_LAND_HSPEED<<endl;
  157.              return LAND_BAD_SPEED;
  158.          }
  159.          return LANDED;
  160.      }
  161.      for (int i = 0; i < surfaceN-1; i++)
  162.      {
  163.          if (landLines[i].collides(prev_x,prev_y,x,y))
  164.          {
  165. //             cerr << "Collision with Land"<<(i+1)<<endl;
  166.              return CRASH;
  167.          }
  168.      }
  169.      return result;
  170.   }
  171.  
  172. double toRadians(double angle){
  173.   return angle * (M_PI / 180);
  174. }
  175.  
  176. double sinDeg(double deg){
  177.   return sin(toRadians(deg));
  178. }
  179.  
  180. double cosDeg(double deg){
  181.   return cos(toRadians(deg));
  182. }  
  183.  
  184.   void ApplyGenoma(const Genoma& g,int step)
  185.   {
  186.     if (g.power[step] > power) power = power+1;
  187.     if (g.power[step] < power) power = power-1;
  188.     if (g.rotation[step] > angle) angle += min(15,g.rotation[step]-angle);
  189.     if (g.rotation[step] < angle) angle -= min(15,angle-g.rotation[step]);
  190.     fuel -= power;    
  191.     ax = cosDeg((double)angle)*power;  
  192.     ay = sinDeg((double)angle)*power+GRAVITY;
  193.     prev_x = x;
  194.     prev_y = y;
  195.     x += vx+ ax*0.5;
  196.     y += vy+ ay*0.5;
  197.     vx += ax;
  198.     vy += ay;
  199.   }  
  200.  
  201.  
  202. };
  203. GameState gamestate,working;
  204. ostream &operator<<(ostream& output, const GameState& gs){
  205.         output << "Lander:P:("<<gs.x<<","<<gs.y<<") V:("<<gs.vx<<","<<gs.vy<<") F:"<<gs.fuel<<" A:"<<gs.angle<<" P:"<<gs.power;
  206.         return output;
  207.     }
  208. bool GameState::Equals(const GameState& gs){
  209.       bool result =  (x==round(gs.x) && y == round(gs.y) && vx == round(gs.vx) && vy == round(gs.vy) && fuel == round(gs.fuel));
  210.       //if (!result)
  211.       {
  212.           cerr << "GameState:"<<*this<<endl;
  213.           cerr << "Working:"<<gs<<endl;
  214.           if (!result) abort();
  215.       }
  216.       return result;
  217.   }
  218.  
  219.  
  220. void TestSim(istream& input)
  221. {
  222.    while(1)
  223.    {
  224.       gamestate.ReadTurn(input);
  225.       if (gamestate.turn > 0)
  226.       {
  227.          cerr << "Compare Result:"<< gamestate.Equals(working)<<endl;
  228.       }      
  229.       else {
  230.           working = gamestate;
  231.       }
  232.       Genoma g;
  233.       g.rotation[0] = (gamestate.turn*3)%180;
  234.       g.power[0] = min(4,((gamestate.turn+1)%14));      
  235.       working.ApplyGenoma(g,0);                
  236.       cerr << "isLanded="<<working.isLanded()<<endl;
  237.       cout << (g.rotation[0]-90)<<" "<<g.power[0]<<endl;
  238.    }
  239. }
  240.  
  241.  
  242. int main(){
  243.    srand(time(0));
  244.    gamestate.ReadConfig(cin);
  245.    TestSim(cin);
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement