Пример #1
0
 public void AddRequirement(Chemical chemical, int amount)
 {
     if (requirements.ContainsKey(chemical))
     {
         requirements[chemical] += amount;
     }
     else
     {
         requirements.Add(chemical, amount);
     }
 }
Пример #2
0
        private Chemical GetOrAddChemical(string id)
        {
            Chemical chemical = null;

            if (availableChemicals.ContainsKey(id))
            {
                chemical = availableChemicals[id];
            }
            else
            {
                chemical = new Chemical(id);
                availableChemicals.Add(id, chemical);
            }
            return(chemical);
        }
Пример #3
0
        public override void Initialize()
        {
            base.Initialize();

            availableChemicals = new Dictionary <string, Chemical>();

            // Add additional space for regex
            for (int i = 0; i < lines.Length; i++)
            {
                lines[i] = " " + lines[i];
            }

            Regex searchExpression = new Regex(@"( \d* )(\w*)", RegexOptions.Compiled);

            for (int i = 0; i < lines.Length; i++)
            {
                MatchCollection collection       = searchExpression.Matches(lines[i]);
                Chemical        chemical         = null;
                Chemical        requiredChemical = null;

                for (int j = collection.Count - 1; j >= 0; j--)
                {
                    Match  match  = collection[j];
                    string id     = match.Groups[2].ToString();
                    int    amount = int.Parse(match.Groups[1].ToString());
                    if (j == collection.Count - 1)
                    {
                        chemical = GetOrAddChemical(id);
                        chemical.SetDefaultQuantity(amount);
                    }
                    else
                    {
                        requiredChemical = GetOrAddChemical(id);

                        chemical.AddRequirement(requiredChemical, amount);
                    }
                }
            }

            Debug();
        }
Пример #4
0
        public override string GetSolution()
        {
            Chemical ore  = availableChemicals["ORE"];
            Chemical fuel = availableChemicals["FUEL"];

            long fuelAmount = 0;

            while (true)
            {
                fuel.Produce(1);

                if (ore.AvailableAmount > 1000000000000)
                {
                    break;
                }

                fuelAmount++;
            }

            return(fuelAmount.ToString());
        }