/// <summary> /// Reduces the specified snail number. /// </summary> /// <param name="number">The snail number to reduce.</param> /// <returns> /// The reduced snail number. /// </returns> internal static string Reduce(string number) { SnailPair pair = ParseRaw(number); pair = pair.Reduce(); return(pair.ToString()); }
/// <summary> /// Calculates the sum of the specified snail numbers. /// </summary> /// <param name="numbers">The snail numbers to sum.</param> /// <returns> /// The sum of the two snail numbers. /// </returns> internal static string SumValues(params string[] numbers) { List <SnailPair> pairs = ParseRaw(numbers); SnailPair sum = pairs.Aggregate((x, y) => (x + y).Reduce()); return(sum.ToString()); }
private static List <SnailPair> ParseRaw(IList <string> numbers) { var pairs = new List <SnailPair>(numbers.Count); foreach (string number in numbers) { SnailPair parsed = ParseRaw(number); pairs.Add(parsed); } return(pairs); }
/// <summary> /// Calculates the sum of the specified snail numbers. /// </summary> /// <param name="numbers">The snail numbers to sum.</param> /// <returns> /// The magnitude of the sum of the snail numbers and the /// largest magnitude of any two of the snail numbers. /// </returns> public static (int MagnitudeOfSum, int LargestSumMagnitude) Sum(IList <string> numbers) { List <SnailPair> pairs = ParseRaw(numbers); SnailPair sum = pairs.Aggregate((x, y) => (x + y).Reduce()); var magnitudes = new List <int>(pairs.Count * pairs.Count * 2); for (int i = 0; i < pairs.Count - 1; i++) { for (int j = 0; j < pairs.Count; j++) { SnailPair x = pairs[i]; SnailPair y = pairs[j]; magnitudes.Add((x + y).Reduce().Magnitude()); magnitudes.Add((y + x).Reduce().Magnitude()); } } return(sum.Magnitude(), magnitudes.Max()); }
/// <summary> /// Gets the magnitude of the specified snail number. /// </summary> /// <param name="number">The snail number to get the magnitude of.</param> /// <returns> /// The magnitude of the snail number. /// </returns> internal static int Magnitude(string number) { SnailPair pair = ParseRaw(number); return(pair.Magnitude()); }