public int GetFourDigitDisplay() { List <string> signalPatterns = SignalPatterns; Dictionary <string, int>?numbers = signalPatterns.ToDictionary(sp => sp, _ => - 1); string[] patterns = new string[10]; SetPatternNumber(signalPatterns.Where(sp => sp.Length == 2).Single(), 1); SetPatternNumber(signalPatterns.Where(sp => sp.Length == 3).Single(), 7); SetPatternNumber(signalPatterns.Where(sp => sp.Length == 4).Single(), 4); SetPatternNumber(signalPatterns.Where(sp => sp.Length == 7).Single(), 8); signalPatterns.RemoveAll(s => patterns.Contains(s)); foreach (string signalPattern in signalPatterns.Where(sp => sp.Length == 6)) { if (signalPattern.ContainsAllWires(patterns[4])) { SetPatternNumber(signalPattern, 9); } else if (signalPattern.ContainsAllWires(patterns[7])) { SetPatternNumber(signalPattern, 0); } else { SetPatternNumber(signalPattern, 6); } } signalPatterns.RemoveAll(s => patterns.Contains(s)); foreach (string signalPattern in signalPatterns) { if (signalPattern.ContainsAllWires(patterns[1])) { SetPatternNumber(signalPattern, 3); } else if (patterns[6].ContainsAllWires(signalPattern)) { SetPatternNumber(signalPattern, 5); } else { SetPatternNumber(signalPattern, 2); } } return(int.Parse(String.Join("", OutputValues.Select(o => numbers[o])))); void SetPatternNumber(string signalPattern, int number) { patterns[number] = signalPattern; numbers[signalPattern] = number; } }