public void TestCountDependencies()
        {
            List <Chemical> inA = new List <Chemical>()
            {
                new Chemical(1, "C")
            };
            Chemical outA = new Chemical(1, "A");
            Reaction a    = new Reaction(inA, outA);

            List <Chemical> inB = new List <Chemical>()
            {
                new Chemical(1, "C")
            };
            Chemical outB = new Chemical(1, "B");
            Reaction b    = new Reaction(inB, outB);

            List <Chemical> inC = new List <Chemical>()
            {
                new Chemical(1, "ORE")
            };
            Chemical outC = new Chemical(1, "C");
            Reaction c    = new Reaction(inC, outC);

            List <Reaction> reactions = new List <Reaction>();

            reactions.Add(a);
            reactions.Add(b);
            reactions.Add(c);

            Nanofactory n = new Nanofactory();

            Assert.That(n.CountDependencies(a, reactions), Is.EqualTo(0));
            Assert.That(n.CountDependencies(b, reactions), Is.EqualTo(0));
            Assert.That(n.CountDependencies(c, reactions), Is.EqualTo(2));
        }
示例#2
0
        private long CountRequiredOres(long requiredFuel, string[] input)
        {
            Nanofactory     n         = new Nanofactory();
            List <Reaction> reactions = n.ParseInput(input);

            Reaction fuelR = reactions.Find(x => x.Output.Name == "FUEL");

            reactions.Remove(fuelR);

            fuelR.Output.Units *= requiredFuel;
            fuelR.Inputs.ForEach(i => i.Units *= requiredFuel);

            while (reactions.Count > 0)
            {
                for (int i = 0; i < reactions.Count;)
                {
                    Reaction r = reactions[i];

                    if (n.CanCombine(fuelR, r) && n.CountDependencies(r, reactions) == 0)
                    {
                        n.CombineReactions(fuelR, r);
                        n.CombineInputs(fuelR);
                        reactions.RemoveAt(i);
                    }
                    else
                    {
                        i++;
                    }
                }
            }

            return(fuelR.Inputs[0].Units);
        }
        public void TestParseInput()
        {
            string[] input = new string[]   {
                "1 A => 1 B", "1 C, 1 D => 1 E"
            };
            Nanofactory     n         = new Nanofactory();
            List <Reaction> reactions = n.ParseInput(input);

            Assert.That(reactions.Count, Is.EqualTo(2));
        }
        public void TestParseReactionSingleInput()
        {
            string      entry = "32 ORE => 28 A";
            Nanofactory n     = new Nanofactory();

            Reaction reaction = n.ParseReaction(entry);

            Assert.That(reaction, Is.Not.EqualTo(null));
            Assert.That(reaction.Inputs.Count, Is.EqualTo(1));
            Assert.That(reaction.Inputs[0].Units, Is.EqualTo(32));
            Assert.That(reaction.Inputs[0].Name, Is.EqualTo("ORE"));
            Assert.That(reaction.Output.Units, Is.EqualTo(28));
            Assert.That(reaction.Output.Name, Is.EqualTo("A"));
        }
        public void TestCombineInputs()
        {
            List <Chemical> inputs = new List <Chemical>();

            inputs.Add(new Chemical(1, "A"));
            inputs.Add(new Chemical(2, "A"));

            Reaction    reaction = new Reaction(inputs, null);
            Nanofactory n        = new Nanofactory();

            n.CombineInputs(reaction);

            Assert.That(reaction.Inputs.Count, Is.EqualTo(1));
            Assert.That(reaction.Inputs[0].Units, Is.EqualTo(3));
        }
        public void TestParseReactionMultipleInputs()
        {
            string      entry    = "1 A, 2 B, 3 C => 4 D";
            Nanofactory n        = new Nanofactory();
            Reaction    reaction = n.ParseReaction(entry);

            Assert.That(reaction, Is.Not.EqualTo(null));
            Assert.That(reaction.Inputs.Count, Is.EqualTo(3));
            Assert.That(reaction.Inputs[0].Units, Is.EqualTo(1));
            Assert.That(reaction.Inputs[0].Name, Is.EqualTo("A"));
            Assert.That(reaction.Inputs[1].Units, Is.EqualTo(2));
            Assert.That(reaction.Inputs[1].Name, Is.EqualTo("B"));
            Assert.That(reaction.Inputs[2].Units, Is.EqualTo(3));
            Assert.That(reaction.Inputs[2].Name, Is.EqualTo("C"));
            Assert.That(reaction.Output.Units, Is.EqualTo(4));
            Assert.That(reaction.Output.Name, Is.EqualTo("D"));
        }
        public void TestCanCombine()
        {
            List <Chemical> inA = new List <Chemical>()
            {
                new Chemical(28, "A")
            };
            Chemical outA = new Chemical(1, "FUEL");
            Reaction a    = new Reaction(inA, outA);

            List <Chemical> inB = new List <Chemical>()
            {
                new Chemical(10, "ORE")
            };
            Chemical outB = new Chemical(10, "A");
            Reaction b    = new Reaction(inB, outB);

            Nanofactory n = new Nanofactory();

            Assert.That(n.CanCombine(b, a), Is.EqualTo(false));
            Assert.That(n.CanCombine(a, b), Is.EqualTo(true));
        }
示例#8
0
        static void Main(string[] args)
        {
            var         oreResourcesAvailable = Constants.MAX_RESOURCES;
            Nanofactory n = new Nanofactory();

            n.inputAsDict = puzzleInputToDict(Constants.INPUT_FILENAME);
            var oreCount       = 0;
            var iterationCount = 0;

            while (oreResourcesAvailable > 0)
            {
                n.inputAsDict         = puzzleInputToDict(Constants.INPUT_FILENAME);
                oreCount              = n.getOreCount();
                oreResourcesAvailable = oreResourcesAvailable - oreCount;
                if (oreResourcesAvailable > 0) //without this statement, iterationCount value was 1 too many
                {
                    iterationCount++;
                }
            }
            Console.WriteLine($"{iterationCount} units of fuel produced");
        }
        public void TestCombineReactions()
        {
            List <Chemical> inA = new List <Chemical>()
            {
                new Chemical(28, "A")
            };
            Chemical outA = new Chemical(1, "FUEL");
            Reaction a    = new Reaction(inA, outA);

            List <Chemical> inB = new List <Chemical>()
            {
                new Chemical(10, "ORE")
            };
            Chemical outB = new Chemical(10, "A");
            Reaction b    = new Reaction(inB, outB);

            Nanofactory n = new Nanofactory();

            n.CombineReactions(a, b);
            Assert.That(a.Inputs[0].Units, Is.EqualTo(30));
            Assert.That(a.Inputs[0].Name, Is.EqualTo("ORE"));
        }
示例#10
0
        public void MaximumAmountOfFuelWithTrilionOre(string fileName, long expectedFuel)
        {
            var nanofactory = new Nanofactory(fileName);

            var OreForOneFuel = nanofactory.MinimumOreForOneFuel();

            var results = new List <(long Fuel, long Ore)>();

            const long trilionOre   = 1000000000000;
            var        fuel1        = trilionOre / OreForOneFuel;
            var        requiredOre1 = nanofactory.MinimumOreForFuel(fuel1);

            results.Add((fuel1, requiredOre1));

            long fuel2 = fuel1 * 2;
            long requiredOre2;
            long delta;

            do
            {
                requiredOre2 = nanofactory.MinimumOreForFuel(fuel2);
                results.Add((fuel2, requiredOre2));

                delta = Math.Abs(fuel2 - fuel1) / 2;
                fuel1 = fuel2;
                if (requiredOre2 > trilionOre)
                {
                    delta  = delta > 0 ? delta : 1;
                    fuel2 -= delta;
                }
                else
                {
                    fuel2 += delta;
                }
            }while (delta > 0);

            Assert.Equal(expectedFuel, results.FindLast(result => result.Ore < trilionOre).Fuel);
        }
示例#11
0
        public void MinimumOreForOneFuelTest(string fileName, long expectedOre)
        {
            var nanofactory = new Nanofactory(fileName);

            Assert.Equal(expectedOre, nanofactory.MinimumOreForOneFuel());
        }