diff --git a/Day14/Day14.cs b/Day14/Day14.cs index fb5db5c..7eb3169 100644 --- a/Day14/Day14.cs +++ b/Day14/Day14.cs @@ -10,14 +10,14 @@ namespace Advent private class Reagent { public string Name { get; } - public int Count { get; } + public long Count { get; } public Reagent(string s) { var parts = s.Split(' '); Name = parts[1]; - Count = int.Parse(parts[0]); + Count = long.Parse(parts[0]); } } @@ -42,7 +42,7 @@ namespace Advent } private static readonly Dictionary Reactions = new Dictionary(); - private static readonly Dictionary Inventory = new Dictionary(); + private static readonly Dictionary Inventory = new Dictionary(); public static void Execute() { @@ -53,30 +53,46 @@ namespace Advent var reaction = new Reaction(line); Reactions[reaction.Result.Name] = reaction; - - Inventory[reaction.Result.Name] = 0; } var oreRequired = GetRequiredOre("FUEL", 1); Console.WriteLine($"Ore: {oreRequired}"); + + // Too lazy to do a binary search - find a decent starting point and go from there. It finished + // in the few minutes it took to get coffee - good enough! + + const long oreLimit = 1000000000000; + var fuel = (long)Math.Floor((decimal)oreLimit / oreRequired); + + while (true) + { + oreRequired = GetRequiredOre("FUEL", fuel); + + if (oreRequired > oreLimit) + break; + + fuel++; + } + + Console.WriteLine($"Fuel: {fuel - 1}"); } - private static int GetRequiredOre(string reagentName, int amountRequired) + private static long GetRequiredOre(string reagentName, long amountRequired) { // Get the reaction to produce this reagent var reaction = Reactions[reagentName]; // Get what we have already - var inInventory = Inventory[reagentName]; + var inInventory = Inventory.ContainsKey(reagentName) ? Inventory[reagentName] : 0; // Figure out how many reactions are needed - var reactionsRequired = (int)Math.Ceiling((decimal)Math.Max(amountRequired - inInventory, 0) / reaction.Result.Count); + var reactionsRequired = (long)Math.Ceiling((decimal)Math.Max(amountRequired - inInventory, 0) / reaction.Result.Count); // Set what we have in inventory after Inventory[reagentName] = (reaction.Result.Count * reactionsRequired) - (amountRequired - inInventory); - var oreRequired = 0; + var oreRequired = 0L; // Loop over each reagent foreach (var reagent in reaction.Components)