示例#1
0
文件: Day14.cs 项目: stames/aoc
        public long Part1()
        {
            var         input = InputUtils.GetDayInputLines(14);
            Nanofactory r     = new Nanofactory(input);

            r.RunFactory(new InputOutputChemical {
                Amount = 1, Chemical = "FUEL"
            });

            return(r.OreCount);
        }
示例#2
0
文件: Day14.cs 项目: stames/aoc
        public long Part2()
        {
            var input = InputUtils.GetDayInputLines(14);

            // a trillion ore
            const long oreStorage = 1_000_000_000_000;

            Nanofactory r = new Nanofactory(input);

            long fuelCount = 0;

            // produce lots at a time
            int fuelToProduce = 1000;
            Dictionary <string, int> tempExtra = null;
            long tempOreCount = 0;

            while (fuelToProduce >= 1)
            {
                while (r.OreCount < oreStorage)
                {
                    tempExtra    = new Dictionary <string, int>(r.ExtraMaterials);
                    tempOreCount = r.OreCount;
                    r.RunFactory(new InputOutputChemical()
                    {
                        Amount = fuelToProduce, Chemical = "FUEL"
                    });
                    fuelCount += fuelToProduce;
                }

                // it wasn't an exact match, scale back and rerun
                // the reactions to produce less fuel
                if (fuelToProduce >= 1)
                {
                    r.ExtraMaterials = new Dictionary <string, int>(tempExtra);
                    r.OreCount       = tempOreCount;
                    fuelCount       -= fuelToProduce;
                    fuelToProduce   /= 10;
                }
            }

            return(fuelCount);
        }