mirror of
https://github.com/ckaczor/Advent2019.git
synced 2026-01-13 17:22:15 -05:00
Day 14 - Part 2
This commit is contained in:
@@ -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<string, Reaction> Reactions = new Dictionary<string, Reaction>();
|
||||
private static readonly Dictionary<string, int> Inventory = new Dictionary<string, int>();
|
||||
private static readonly Dictionary<string, long> Inventory = new Dictionary<string, long>();
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user