Beispiel #1
0
        static void Main(string[] args)
        {
            var lines     = File.ReadAllLines("input.txt");
            var reactions = lines.Select(Reaction.Parse);
            var factory   = new NanoFactory(reactions);
            var res1      = factory.GetOreRequirementFor(new ReactionItem(Chemical.FUEL, 1));

            Console.WriteLine($"Part I: {res1}");

            const long trillion = 1_000_000_000_000;
            long       min      = trillion / res1;
            long       max      = trillion;

            while (min < max - 1)
            {
                long med = (min + max) / 2;
                var  ore = factory.GetOreRequirementFor(new ReactionItem(Chemical.FUEL, med));
                if (ore > trillion)
                {
                    max = med;
                }
                else
                {
                    min = med;
                }
            }

            Console.WriteLine($"Part II: {min}");
        }
        public void Part1_Sample1()
        {
            var reactions = new Reaction[]
            {
                Reaction.Parse("10 ORE => 10 A"),
                Reaction.Parse("1 ORE => 1 B"),
                Reaction.Parse("7 A, 1 B => 1 C"),
                Reaction.Parse("7 A, 1 C => 1 D"),
                Reaction.Parse("7 A, 1 D => 1 E"),
                Reaction.Parse("7 A, 1 E => 1 FUEL")
            };

            var factory = new NanoFactory(reactions);
            var res     = factory.GetOreRequirementFor(new ReactionItem(Chemical.FUEL, 1));

            Assert.Equal(31, res);
        }
        public void Part1_Sample2()
        {
            var reactions = new Reaction[]
            {
                Reaction.Parse("9 ORE => 2 A"),
                Reaction.Parse("8 ORE => 3 B"),
                Reaction.Parse("7 ORE => 5 C"),
                Reaction.Parse("3 A, 4 B => 1 AB"),
                Reaction.Parse("5 B, 7 C => 1 BC"),
                Reaction.Parse("4 C, 1 A => 1 CA"),
                Reaction.Parse("2 AB, 3 BC, 4 CA => 1 FUEL")
            };

            var factory = new NanoFactory(reactions);
            var res     = factory.GetOreRequirementFor(new ReactionItem(Chemical.FUEL, 1));

            Assert.Equal(165, res);
        }