// This gives the right answer of 1,184,209 private long ByIterative(string input, long maxOre) { var reactions = new ReactionParser().Parse(input); var topologicalOrder = TheHellDoTheyWant.TopologicalOrder(reactions); var chain = new ReactionChain().GetReactionChain(topologicalOrder, reactions); var ore = chain.all.Last(); int i = 0; while (ore.TotalProduced < maxOre) { i++; if (chain.fuel.Request(1) == 0) { break; } if (i % 1000 == 0) { Console.WriteLine("{0}\t{1}\t{2}", i, chain.fuel.TotalProduced, ore.TotalProduced / 1_000_000_000); } } return(chain.fuel.TotalProduced); }
// I give up. I'm following some guy online. I get less, I get more. I've been doing // this for too long. It's after midnight and I skipped dinner b/c I'm a loser. // I can't even think. Why do I do this shit when I'm tired? public int Run(string input = C14.Challenge.Input) { var reactions = new ReactionParser().Parse(input); var fuel = reactions.Find(r => r.Output.Ingredient == "FUEL"); //reactions.Remove(fuel); var topologicalOrder = TopologicalOrder(reactions); var(oreRequired, iterations) = OreRequired(topologicalOrder, reactions); return(oreRequired); }
// what is the minimum amount of ORE required to produce exactly 1 FUEL public long Run(string input = null) { if (input == null) { input = Input; } //return new TheHellDoTheyWant().Run(input); var reactions = new ReactionParser().Parse(input); var topologicalOrder = TheHellDoTheyWant.TopologicalOrder(reactions); var res = new ReactionChain().Run(topologicalOrder, reactions, pair => pair.fuel.TotalProduced < 1); return(res.totalOre); }
public long Run() { var parser = new ReactionParser(); var input = Input; var allRecipes = parser.Parse(input); var fuel = allRecipes.Find(r => r.Output.Ingredient == "FUEL"); var links = allRecipes.GroupBy(x => x.Output.Ingredient).ToDictionary(x => x.Key, x => x.ToArray()); var ore = "ORE"; /* should have split the name and quantity */ var allPaths = FindRecipes(fuel.Output, ore, links).ToList(); //var totalOre = _nanoFactory.Run(allPaths); var totalOre = new QueueItUpFactory().Run(allPaths); return(totalOre);
// this gives a wrong answer of 925,382 private static long ByJumping(string input, long maxOre) { var reactions = new ReactionParser().Parse(input); var topologicalOrder = TheHellDoTheyWant.TopologicalOrder(reactions); var oneReaction = new ReactionChain().Run(topologicalOrder, reactions, x => x.fuel.TotalProduced < 1); var startingPoint = (int)(maxOre / oneReaction.totalOre); var chain = new ReactionChain().GetReactionChain(topologicalOrder, reactions); var ore = chain.all.Last(); chain.fuel.Request(startingPoint); int i = 0; var midpoint = (maxOre - ore.TotalProduced) / oneReaction.totalOre; // startingPoint / 10; while (ore.TotalProduced < maxOre) { if (chain.fuel.Request(midpoint) == 0) { if (midpoint == 1) { break; } midpoint = (maxOre - ore.TotalProduced) / oneReaction.totalOre; midpoint = Math.Max(midpoint, 1); Console.WriteLine("{0}\t{1}\t{2}", midpoint, chain.fuel.TotalProduced, ore.TotalProduced); } i++; Console.WriteLine("{0}\t{1}\t{2}", i, chain.fuel.TotalProduced, ore.TotalProduced); } return(chain.fuel.TotalProduced); }