// 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); }
// 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); }
// 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); }