Advertisement
Guest User

Prismata Code Challenge April 2019

a guest
Apr 11th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.12 KB | None | 0 0
  1. // C code attempting to solve the Cannons vs Tarsiers challenge
  2. // Liam Fisher 2019
  3.  
  4. #include <stdio.h>
  5.  
  6. int PRISMATA(int fh, int fl, int gh, int g, int t, int TCH, int sF, int TURN){
  7.  
  8.     if (TURN != 0) {
  9.    
  10.         if (t >= (fh + TCH)) // Mariusz Wins
  11.             return TURN;
  12.    
  13.         // MARIUSZ ACTIONS
  14.         fh -= sF; // Fabricator health -= "Shoot Fabricator"
  15.         TCH -= (t-sF);
  16.    
  17.         // Update cannon count
  18.         if (TCH <= 0){
  19.             g = 0;
  20.             TCH = 0;
  21.         }
  22.         else
  23.             g = (TCH + gh - 1) / gh; // hacky way in c to calculate ceil(TCH/gh) using positive integers
  24.    
  25.         // PAWEL ACTIONS
  26.         t -= g;
  27.        
  28.         if (t <= 0) // Pawel Wins
  29.             return -1;
  30.    
  31.         // Fabricator builds a cannon
  32.         if(fh > 0 && fl > 0){
  33.             fl--;
  34.             if (fl == 0)
  35.                 fh = 0;
  36.             g++;
  37.             TCH += gh;
  38.         }  
  39.    
  40.         // OPTIMIZATION //
  41.         // Hard code some cases to reduce number of function calls
  42.         if (t >= (fh + TCH)) // We know Mariusz will win on the next turn
  43.             return TURN+1;
  44.         //////////////////
  45.     }
  46.  
  47.     TURN++;
  48.  
  49.     int MaxFD, MinFD; // Maximum and minimum amount of damage that can be assigned to the fabricator
  50.     if (fh == 0){
  51.         MaxFD = 0;
  52.         MinFD = 0;
  53.     }
  54.     else {
  55.         if (fh >= t)
  56.             MaxFD = t;
  57.         else
  58.             MaxFD = fh;
  59.         if (TCH >= t)
  60.             MinFD = 0;
  61.         else
  62.             MinFD = t-TCH;
  63.     }
  64.  
  65.     int result;
  66.     int j = 0;
  67.     int BestResult = -1;
  68.     for (int i = MinFD; i <= MaxFD; i++){
  69.        
  70.         result = PRISMATA(fh, fl, gh, g, t, TCH, i, TURN);
  71.        
  72.         if ((result > -1 && result < BestResult) || BestResult == -1)
  73.             BestResult = result;
  74.         j++;
  75.     }
  76.  
  77.     return BestResult;
  78. }
  79.  
  80. int main(void)
  81. {
  82.     int T; //
  83.     scanf("%d", &T);
  84.     int fh, fl, gh, g, t;
  85.     int fh_A[T], fl_A[T], gh_A[T], g_A[T], t_A[T];
  86.     int TCH; // Total cannon health
  87.     int Result;
  88.     for (int n = 0; n < T; n++){
  89.             scanf("%d", &fh_A[n]);
  90.             scanf("%d", &fl_A[n]);
  91.             scanf("%d", &gh_A[n]);
  92.             scanf("%d", &g_A[n]);
  93.             scanf("%d", &t_A[n]);
  94.     }
  95.  
  96.     for (int n = 0; n < T; n++){
  97.         fh = fh_A[n];
  98.         fl = fl_A[n];
  99.         gh = gh_A[n];
  100.         g = g_A[n];
  101.         t = t_A[n];
  102.         TCH = gh * g;
  103.  
  104.         Result = PRISMATA(fh, fl, gh, g, t, TCH, 0, 0);
  105.    
  106.         if (Result > -1)
  107.             printf("MARIUSZ %d\n", Result);
  108.         else
  109.             printf("PAWEL\n");
  110.     }
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement