string[] examples = { "mjqjpqmgbljsphdztnvjfqwrcgsmlb", "bvwbjplbgvbhsrlpgdmjqwftvncz", "nppdvjthqldpwncqszvftbrmjlhg", "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", }; int[] uniqueCount = { 4, 14 }; /**** Part 01 ***/ foreach (var unique in uniqueCount) { Console.WriteLine(String.Format("\nFinding based on {0} unique consequetive characters", unique)); // Perform ops on examples foreach (string example in examples) { PrintFirstUniqueIndex(example, unique); } // Do it on the real puzzle string fileName = "puzzle-input.txt"; using (StreamReader reader = File.OpenText(fileName)) { string? line = reader.ReadLine(); if (line != null) { PrintFirstUniqueIndex(line, unique); } else { throw new InvalidDataException(); } } } void PrintFirstUniqueIndex(string str, int uniqueLength) { for (int i = 0; i < str.Length; i++) { var substr = str.Substring(i, uniqueLength); if (substr.Distinct().Count() == uniqueLength) { Console.WriteLine("Unique Index Start: " + (i+uniqueLength)); return; } } throw new InvalidDataException(); }