public string Solve(string input) { string[] inputLines = input.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); int yOffset = inputLines[0].Length; string map = string.Concat(inputLines); // Find the minecarts List <Minecart> minecarts = Minecart.FindInMap(ref map, yOffset); while (true) { // First, sort the minecarts by their position... minecarts = minecarts.OrderBy(x => x.Position).ToList(); // Now move them all... foreach (Minecart current in minecarts) { current.Position += current.NextMove; current.SetNextMove(yOffset, map[current.Position]); // Has it crashed? if (current.GetCrashedIntoMinecart(minecarts) != null) { return($"{current.Position % yOffset},{current.Position / yOffset}"); } } } }
public string Solve(string input) { string[] inputLines = input.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); int yOffset = inputLines[0].Length; string map = string.Concat(inputLines); // Find the minecarts List <Minecart> minecarts = Minecart.FindInMap(ref map, yOffset); // We won't be able to just remove carts from the collection as we go, // so we keep a list of those that have been removed var removedCarts = new List <Minecart>(); while (true) { // First, sort the minecarts by their position... minecarts = minecarts.OrderBy(x => x.Position).ToList(); // Now move the ones that haven't crashed... foreach (Minecart current in minecarts) { if (removedCarts.Contains(current)) { continue; } current.Position += current.NextMove; current.SetNextMove(yOffset, map[current.Position]); // Has it crashed? Minecart crashedIntoMinecart = current.GetCrashedIntoMinecart(minecarts.Where(x => !removedCarts.Contains(x))); if (crashedIntoMinecart != null) { removedCarts.Add(current); removedCarts.Add(crashedIntoMinecart); } } // At the end of the tick - is there only one remaining? if (minecarts.Count - removedCarts.Count == 1) { Minecart lastCart = minecarts.First(x => !removedCarts.Contains(x)); return($"{lastCart.Position % yOffset},{lastCart.Position / yOffset}"); } } }