using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LunediQuiz1 { class Program { static string[] datastring = new string[]{ "0011000000", "0011000100", "0011001100", "0001111100", "0000000001", "1111111111", "0011110010", "0000001111", "0000000010" }; class block { public UInt64 bitblock; public int length; } static List Break(UInt64 data) { var result = new List(); if (data == 0) { return result; } int currentCount = 0; UInt64 previousBitDoubled = ((data & (data - 1))^data); UInt64 currentBlock = 0; while(data != 0) { var newdata = data & (data - 1); // turn the last '1' on the right into 0 var bit = newdata ^ data; if (bit == previousBitDoubled) // if the current bit is at the immediate left of the previous { currentBlock |= bit; currentCount++; } else { result.Add(new block() { bitblock = currentBlock, length = currentCount }); currentBlock = bit; currentCount = 1; } previousBitDoubled = bit << 1; data = newdata; } if (currentCount != 0) { result.Add(new block() { bitblock = currentBlock, length = currentCount }); } return result; } static void Main(string[] args) { UInt64[] data = new UInt64[datastring.Length]; for (int i = 0; i < datastring.Length; i++) { data[i] = Convert.ToUInt64(datastring[i], 2); } var previousLine = Break(data[0]); int max = previousLine.Max(i => i.length); for (int i = 1; i < data.Length; i++) { var currentLine = Break(data[i]); for (int j = 0; j < currentLine.Count; j++) { var currentLineBlock = currentLine[j]; foreach (var block in previousLine) { if ((currentLineBlock.bitblock & block.bitblock) != 0) { currentLineBlock.length += block.length; while (j < (currentLine.Count - 1)) { if ((currentLine[j + 1].bitblock & block.bitblock) != 0) { // merge the current block with the next one and remove the next one // from the list currentLineBlock.length += currentLine[j + 1].length; currentLineBlock.bitblock |= currentLine[j + 1].bitblock; j++; currentLine.RemoveAt(j); } else { break; } } } } } max = Math.Max(max, currentLine.Max(item => item.length)); previousLine = currentLine; } Console.WriteLine(max); } } }