public void Swap(int index1, int index2) { Marble temp = _marbles[index1]; _marbles[index1] = _marbles[index2]; _marbles[index2] = temp; }
public void SortByPosition() { Marble thisMarble = null; // Find west most marble for (int i = 0; i < _marbles.Count; ++i) { if (_marbles[i].PreviousMarble == null) { thisMarble = _marbles[i]; break; } } _marbles.Clear(); while (thisMarble != null) { _marbles.Add(thisMarble); thisMarble = thisMarble.NextMarble; } }
public Tube(string filename) { Filename = filename; if (File.Exists(Filename)) { _marbles = new List <Marble>(); StreamReader reader = File.OpenText(Filename); string line; int marbleIndex = 0; bool checkNum = false; bool setCheckMarble = false; while ((line = reader.ReadLine()) != null) { string delimiters = "" + Constants.WestDelimiter + Constants.EastDelimiter + Constants.CheckDelimiter + Constants.CheckDistanceDelimiter; string[] parts = Regex.Split(line, @"([" + delimiters + @"])"); double p; bool firstNumber = true; Marble thisMarble = null; foreach (string part in parts) { if (double.TryParse(part, out p)) { if (firstNumber) { _length = (UInt64)(p * Constants.DistanceMultiplier); firstNumber = false; } else if (checkNum) { _checkMarble = (int)p - 1; checkNum = false; } else if (setCheckMarble) { _marbles[_checkMarble].TravelDistanceMillimeters = p; _marbles[_checkMarble].RecordDistance = true; setCheckMarble = false; } else { thisMarble.Position = (UInt64)(p * Constants.DistanceMultiplier); _marbles.Add(thisMarble); if (marbleIndex > 0) { thisMarble.PreviousMarble = _marbles[marbleIndex - 1]; thisMarble.PreviousMarble.NextMarble = thisMarble; } marbleIndex++; } } else { switch (part[0]) { case Constants.CheckDelimiter: checkNum = true; break; case Constants.CheckDistanceDelimiter: setCheckMarble = true; break; case Constants.WestDelimiter: thisMarble = new Marble(0, true); break; case Constants.EastDelimiter: thisMarble = new Marble(0, false); break; } } } } reader.Close(); } else { Console.Write("ERROR: Filename " + Filename + " does not exist!"); } Console.WriteLine("^ Total Marbles:" + _marbles.Count + " " + DateTime.Now); }
private static int CompareMarblesByDistanceTraveled(Marble a, Marble b) { return((int)(a.TravelDistance - b.TravelDistance)); }