コード例 #1
0
ファイル: Program.cs プロジェクト: terjew/AdventOfCode2019
        public ReactionComponent(string definition)
        {
            var pair = definition.Trim().Split(" ");

            Count    = int.Parse(pair[0]);
            Chemical = Chemical.Get(pair[1]);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: terjew/AdventOfCode2019
        public static long CalculateMaximumFuel()
        {
            long ore   = 1000000000000;
            var  fuel  = Chemical.Get("FUEL");
            var  store = Chemical.Chemicals.Select(kvp => kvp.Value).ToDictionary(c => c, c => 0L);

            Stopwatch sw    = Stopwatch.StartNew();
            long      count = 0;

            while (ore > 0)
            {
                int tryProduce = 1;
                //if (ore > 5000000000000) tryProduce = 1000;
                //if (ore > 2000000000000) tryProduce = 100;
                //if (ore > 1000000000000) tryProduce = 10;
                var required = CountRequiredOre(fuel, tryProduce, store);
                if (ore < required)
                {
                    break;
                }
                ore -= required;
                count++;

                if (sw.Elapsed > TimeSpan.FromSeconds(1))
                {
                    long   spent   = 1000000000000 - ore;
                    double percent = (double)spent / 1000000000000;
                    Console.WriteLine($"{percent * 100}%");
                    sw.Restart();
                }
            }
            return(count);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: terjew/AdventOfCode2019
        public static long CountOreForOneFuel()
        {
            var fuel  = Chemical.Get("FUEL");
            var store = Chemical.Chemicals.Select(kvp => kvp.Value).ToDictionary(c => c, c => 0L);

            var amount = CountRequiredOre(fuel, 1, store);

            return(amount);
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: terjew/AdventOfCode2019
        static long CountRequiredOre(Chemical chemical, long required, Dictionary <Chemical, long> store)
        {
            if (chemical.Name == "ORE")
            {
                return(required);
            }

            var synthesis = chemical.Synthesis;
            var output    = synthesis.Produces;
            var stored    = store[chemical];

            if (stored > 0)
            {
                if (stored > required)
                {
                    store[chemical] = stored - required;
                    return(0);
                }
                else
                {
                    store[chemical] = 0;
                    required       -= stored;
                }
            }
            var reactionCount = (int)Math.Ceiling(required / (float)output.Count);
            var produced      = reactionCount * output.Count;

            if (produced > required)
            {
                store[chemical] += (produced - required);
            }

            long ore = 0;

            foreach (var inputComponent in synthesis.Consumes)
            {
                ore += CountRequiredOre(inputComponent.Chemical, inputComponent.Count * reactionCount, store);
            }

            return(ore);
        }
コード例 #5
0
 public static RecipeLine GetForOutputChemical(this IEnumerable <RecipeLine> lines, Chemical chemical)
 {
     return(lines.Single(l => l.IsForOutputChemical(chemical)));
 }
コード例 #6
0
 public static bool IsForOutputChemical(this RecipeLine line, Chemical chemical)
 {
     return(line.Output.Chemical.Equals(chemical));
 }