Advertisement
ZeroByter

CodinGame Puzzle Code #2

Jun 1st, 2018
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.98 KB | None | 0 0
  1. class Link
  2. {
  3.     //...
  4.  
  5.     //gets an ordered list of all factories sorted by distance from `sourceFactory`
  6.     public static IOrderedEnumerable<KeyValuePair<Factory, int>> GetFactoriesByDistance(Factory sourceFactory)
  7.     {
  8.         Dictionary<Factory, int> sortedFactories = new Dictionary<Factory, int>();
  9.  
  10.         foreach(Link link in Links)
  11.         {
  12.             if (link.source != sourceFactory.id) continue; //only looking at links from that originate sourceFactory
  13.             Factory targetFactory = Factory.Factories[link.target];
  14.             sortedFactories.Add(targetFactory, link.distance);
  15.         }
  16.  
  17.         return sortedFactories.OrderBy(key => key.Value); //return the sorted 'list'
  18.     }
  19.  
  20.     public static Factory GetTargetFactory(Factory sourceFactory)
  21.     {
  22.         foreach (KeyValuePair<Factory, int> factoryPair in Link.GetFactoriesByDistance(Factory.FriendlyFactories.Values.First())) //get the sorted 'list'
  23.         {
  24.             Factory targetFactory = factoryPair.Key; //simple placeholder variable
  25.  
  26.             //apply logic to get the best enemy factory to target
  27.             if (targetFactory.owner == 1) continue; //cant be targeting our own factory
  28.             if (Troop.GetCountOfFriendlyTroopsTargetingFactory(targetFactory) > targetFactory.finalNumberOfCyborgs + 1) continue; //check if there is already enough friendly troops targeting this factory
  29.             if (sourceFactory.numberOfCyborgs < targetFactory.finalNumberOfCyborgs - 3) continue; //if we dont have enough troops to capture the planet, we move on
  30.             if (factoryPair.Value < Bomb.TurnsUntilBombHitsFactory(targetFactory)) return null; //THIS HAS TO BE THE LAST CHECK - if we are going to get to this target factory faster than a bomb would (meaning we would suffer from it's effects after we capture the factory) then we return null instead of continuing because we still want to target this factory, just later
  31.             return targetFactory;
  32.         }
  33.  
  34.         return null;
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement