Exemplo n.º 1
0
        public static void PartTwo()
        {
            string input = InputHelper.GetInputFromFile("14");

            string[]      lines   = input.Split(Environment.NewLine);
            List <Recipe> recipes = lines.Select(l => new Recipe(l)).ToList();

            Console.WriteLine("Recipes stored.");
            Console.WriteLine("Recipe for FUEL is : " + recipes.First(r => r.Product.Id == "FUEL"));

            long increment    = 1000000;
            long total        = increment;
            long previousUsed = 0;
            long used         = 0;

            while (increment > 0)
            {
                RecipeRobot bot = new RecipeRobot(recipes);
                bot.Make(total, "FUEL");
                used = bot.GetUsed("ORE");
                if (used > 1000000000000)
                {
                    total     -= increment;
                    increment /= 10;
                    used       = previousUsed;
                }
                else
                {
                    total += increment;
                }
                previousUsed = used;
            }

            Console.WriteLine("Produced " + total + " FUEL using " + used + " ORE");
        }
Exemplo n.º 2
0
        public void Make_Should_Track_How_Many_Of_Given_Element_Is_Required_To_Make_Given_Product_Based_On_Recipes()
        {
            List <Recipe> recipes = new List <Recipe>
            {
                new Recipe("1 ORE => 1 FUEL")
            };
            RecipeRobot botTested = new RecipeRobot(recipes);

            botTested.Make(1, "FUEL");

            botTested.GetUsed("ORE").Should().Be(1);
        }
Exemplo n.º 3
0
        public void Make_Should_Cascade_Recipes_Down_To_Bottom()
        {
            List <Recipe> recipes = new List <Recipe>
            {
                new Recipe("1 ORE => 1 KEBAB"),
                new Recipe("1 KEBAB => 1 FUEL"),
            };
            RecipeRobot botTested = new RecipeRobot(recipes);

            botTested.Make(1, "FUEL");

            botTested.GetUsed("ORE").Should().Be(1);
        }
Exemplo n.º 4
0
        public void Make_Should_Handle_Leftovers()
        {
            List <Recipe> recipes = new List <Recipe>
            {
                new Recipe("1 ORE => 2 KEBAB"),
                new Recipe("1 ORE, 1 KEBAB => 1 SHISH"),
                new Recipe("1 KEBAB, 1 SHISH => 1 FUEL"),
            };
            RecipeRobot botTested = new RecipeRobot(recipes);

            botTested.Make(1, "FUEL");

            botTested.GetUsed("ORE").Should().Be(2);
        }
Exemplo n.º 5
0
        public void Make_Should_Handle_Reagent_Quantities()
        {
            List <Recipe> recipes = new List <Recipe>
            {
                new Recipe("1 ORE => 1 KEBAB"),
                new Recipe("1 ORE => 1 SHISH"),
                new Recipe("2 KEBAB, 1 SHISH => 1 FUEL"),
            };
            RecipeRobot botTested = new RecipeRobot(recipes);

            botTested.Make(1, "FUEL");

            botTested.GetUsed("ORE").Should().Be(3);
        }
Exemplo n.º 6
0
        public static void PartOne()
        {
            string input = InputHelper.GetInputFromFile("14");

            string[]      lines   = input.Split(Environment.NewLine);
            List <Recipe> recipes = lines.Select(l => new Recipe(l)).ToList();

            Console.WriteLine("Recipes stored.");
            Console.WriteLine("Recipe for FUEL is : " + recipes.First(r => r.Product.Id == "FUEL"));

            RecipeRobot bot = new RecipeRobot(recipes);

            bot.Make(1, "FUEL");
            Console.WriteLine("Used : " + bot.UsedElements.Count(e => e.Id == "ORE") + " ORE");
        }
Exemplo n.º 7
0
        public void Make_Should_Handle_Complex_Recipes()
        {
            List <Recipe> recipes = new List <Recipe>
            {
                new Recipe("9 ORE => 2 A"),
                new Recipe("8 ORE => 3 B"),
                new Recipe("7 ORE => 5 C"),
                new Recipe("3 A, 4 B => 1 AB"),
                new Recipe("5 B, 7 C => 1 BC"),
                new Recipe("4 C, 1 A => 1 CA"),
                new Recipe("2 AB, 3 BC, 4 CA => 1 FUEL")
            };
            RecipeRobot botTested = new RecipeRobot(recipes);

            botTested.Make(1, "FUEL");

            botTested.GetUsed("ORE").Should().Be(165);
        }
Exemplo n.º 8
0
        public void Make_Should_Handle_Great_Amounts_Quickly()
        {
            List <Recipe> recipes = new List <Recipe>
            {
                new Recipe("9 ORE => 2 A"),
                new Recipe("8 ORE => 3 B"),
                new Recipe("7 ORE => 5 C"),
                new Recipe("3 A, 4 B => 1 AB"),
                new Recipe("5 B, 7 C => 1 BC"),
                new Recipe("4 C, 1 A => 1 CA"),
                new Recipe("2 AB, 3 BC, 4 CA => 1 FUEL")
            };
            RecipeRobot botTested = new RecipeRobot(recipes);

            Expression <Action <RecipeRobot> > action = bot => bot.Make(10000000000, "FUEL");

            botTested.ExecutionTimeOf(action).Should().BeLessOrEqualTo(500.Milliseconds());
        }