Example #1
0
        private static bool Build(Dictionary <Chemical, List <Chemical> > chemicalOperations, Dictionary <string, long> stock, long chemicalAmount, string chemicalName, ref long fuels)
        {
            if (chemicalName.Equals(FUEL))
            {
                fuels += chemicalAmount;
            }

            stock.TryGetValue(chemicalName, out long storedAmount);
            stock[chemicalName] = Math.Max(storedAmount - chemicalAmount, 0);
            chemicalAmount      = Math.Max(chemicalAmount - storedAmount, 0);

            if (chemicalAmount == 0)
            {
                return(false);
            }

            if (chemicalName.Equals(ORE))
            {
                return(true);
            }

            var      chemicalOperation = chemicalOperations.First(x => x.Key.Name.Equals(chemicalName));
            Chemical chemNeeded        = chemicalOperation.Key;
            long     countRepetitions  = (long)Math.Ceiling(chemicalAmount / (double)chemNeeded.Amount);

            countRepetitions = Math.Max(1, countRepetitions);

            var inputs = chemicalOperation.Value;

            var boolReturn = false;

            foreach (var inp in inputs)
            {
                boolReturn = Build(chemicalOperations, stock, inp.Amount * countRepetitions, inp.Name, ref fuels);
            }

            long waste = (chemNeeded.Amount * countRepetitions) - chemicalAmount;

            if (waste > 0)
            {
                stock[chemicalName] += waste;
            }
            return(boolReturn);
        }
Example #2
0
        private static void OreNeeded(Dictionary <Chemical, List <Chemical> > chemicalOperations, string chemicalName, long chemicalAmount, Dictionary <string, long> stock, ref long oreUsed)
        {
            stock.TryGetValue(chemicalName, out long storedAmount);
            stock[chemicalName] = Math.Max(storedAmount - chemicalAmount, 0);
            chemicalAmount      = Math.Max(chemicalAmount - storedAmount, 0);

            if (chemicalAmount == 0)
            {
                return;
            }

            if (chemicalName.Equals(ORE))
            {
                oreUsed += chemicalAmount;
                return;
            }

            var      chemicalOperation = chemicalOperations.First(x => x.Key.Name.Equals(chemicalName));
            Chemical chemNeeded        = chemicalOperation.Key;
            long     countRepetitions  = (long)Math.Ceiling(chemicalAmount / (double)chemNeeded.Amount);

            countRepetitions = Math.Max(1, countRepetitions);

            var inputs = chemicalOperation.Value;

            foreach (var inp in inputs)
            {
                OreNeeded(chemicalOperations, inp.Name, inp.Amount * countRepetitions, stock, ref oreUsed);
            }

            long waste = (chemNeeded.Amount * countRepetitions) - chemicalAmount;

            if (waste > 0)
            {
                stock[chemicalName] += waste;
            }
        }