Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.96 KB | None | 0 0
  1. public class Nanofactory
  2.     {
  3.         private Dictionary<string, Chemical> _nameToChemicalResult;
  4.         private Dictionary<Chemical, Reaction> _reactionMap;
  5.         private Dictionary<string, int> _spares;
  6.         private long _oreCount;
  7.  
  8.         public Nanofactory(string[] inputLines)
  9.         {
  10.             _nameToChemicalResult = new Dictionary<string, Chemical>();
  11.             _reactionMap = new Dictionary<Chemical, Reaction>();
  12.             foreach (string line in inputLines)
  13.             {
  14.                 string[] split = line.Split(" => ");
  15.                 string[] required = split[0].Split(", ");
  16.                 string[] result = split[1].Split(" ");
  17.  
  18.                 Chemical resultingChemical = new Chemical(result[1], int.Parse(result[0]));
  19.  
  20.                 List<Chemical> reactionComponents = new List<Chemical>();
  21.                 foreach (string req in required)
  22.                 {
  23.                     string[] s2 = req.Split(" ");
  24.                     Chemical r = new Chemical(s2[1], int.Parse(s2[0]));
  25.                     reactionComponents.Add(r);
  26.                 }
  27.                 Reaction react = new Reaction(reactionComponents.ToArray());
  28.  
  29.                 _nameToChemicalResult.Add(resultingChemical.name, resultingChemical);
  30.                 _reactionMap.Add(resultingChemical, react);
  31.             }
  32.         }
  33.  
  34.         public long GetRequiredOreForFuel(int fuelCount)
  35.         {
  36.             _oreCount = 0;
  37.             _spares = new Dictionary<string, int>();
  38.  
  39.             MakeMaterials("FUEL", fuelCount);
  40.  
  41.             return _oreCount;
  42.         }
  43.  
  44.         private void MakeMaterials(string name, int amountNeeded)
  45.         {
  46.             Chemical key = _nameToChemicalResult[name];
  47.             Reaction reaction = _reactionMap[key];
  48.  
  49.             int numSpares = 0;
  50.             _spares.TryGetValue(name, out numSpares);
  51.  
  52.             int minimumCanMake = key.amount;
  53.             int willMake = minimumCanMake;
  54.             int difference = 0;
  55.             while (willMake + numSpares < amountNeeded)
  56.             {
  57.                 willMake += minimumCanMake;
  58.             }
  59.             if (willMake < amountNeeded)
  60.             {
  61.                 difference = amountNeeded - willMake;
  62.                 willMake += difference;
  63.                 _spares[name] -= difference;
  64.                 Console.WriteLine($"  =>set spares[{name}] to {_spares[name]}");
  65.             }
  66.             int actuallyMake = willMake - difference;
  67.             Console.WriteLine($"Will make {willMake} ({actuallyMake}) of {name}, using {difference} spares");
  68.  
  69.             foreach (Chemical required in reaction.required)
  70.             {
  71.                 if (required.name.Equals("ORE"))
  72.                 {
  73.                     long old = _oreCount;
  74.                     _oreCount += actuallyMake * required.amount / key.amount;
  75.                     Console.WriteLine($"    ORE ({old}) ----> ({_oreCount})");
  76.                 }
  77.                 else
  78.                 {
  79.                     MakeMaterials(required.name, actuallyMake * required.amount / key.amount);
  80.                 }
  81.             }
  82.  
  83.             if (!_spares.ContainsKey(name))
  84.             {
  85.                 _spares.Add(name, 0);
  86.             }
  87.             if (willMake > amountNeeded)
  88.             {
  89.                 _spares[name] += willMake - amountNeeded;
  90.                 Console.WriteLine($"  =>set spares[{name}] to {_spares[name]}");
  91.             }
  92.         }
  93.  
  94.         public struct Chemical
  95.         {
  96.             public string name;
  97.             public int amount;
  98.             public Chemical(string name, int amount)
  99.             {
  100.                 this.name = name;
  101.                 this.amount = amount;
  102.             }
  103.         }
  104.  
  105.         public class Reaction
  106.         {
  107.             public List<Chemical> required;
  108.             public Reaction(params Chemical[] results)
  109.             {
  110.                 required = new List<Chemical>();
  111.                 required.AddRange(results);
  112.             }
  113.         }
  114.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement