static void PartTwo(string input) { var usedSquares = new List <UsedGridCell>(); for (int j = 0; j < 128; j++) { var ba = KnotHash.HashAsBoolArr(input + "-" + j); for (int i = 0; i < 128; i++) { if (ba[i]) { usedSquares.Add(new UsedGridCell(j, i)); } } } int groupId = 0; while (usedSquares.Any(s => !s.Visited)) { usedSquares.First(s => !s.Visited).VisitAdjacent(usedSquares, groupId++); } Console.WriteLine("There are " + groupId + " regions present."); }
static void Main(string[] args) { StreamReader file = new StreamReader("input.txt"); string baseKey; baseKey = file.ReadLine(); file.Close(); KnotHash knotHash = new KnotHash(); List <int[]> diskGrid = new List <int[]>(); int usedGridCells = 0; int regionCount = 0; for (int i = 0; i < 128; i++) { int[] arr = new int[128]; int baseIndex = 0; string hash = knotHash.GetHashString(string.Format("{0}-{1}", baseKey, i)); foreach (char c in hash) { byte b = Convert.ToByte(c.ToString(), 16); for (int k = 3; k >= 0; k--) { if ((b & (1 << k)) > 0) { usedGridCells++; arr[baseIndex++] = -1; } else { arr[baseIndex++] = 0; } } } diskGrid.Add(arr); } for (int y = 0; y < 128; y++) { for (int x = 0; x < 128; x++) { if (diskGrid[y][x] == -1) { FillRegion(++regionCount, x, y, diskGrid); } } } Console.WriteLine(string.Format("Used Cells: {0}", usedGridCells)); Console.WriteLine(string.Format("Regions: {0}", regionCount)); Console.ReadLine(); }
static void Main(string[] args) { KnotHash kh = new KnotHash(); GridCreator d = new GridCreator(kh); Defragmentor f = new Defragmentor(); var square = d.Generate("nbysizxe"); // Part one Console.WriteLine(f.GetUsedSpace(square)); // Part two Console.WriteLine(f.CountUsedGroups(square)); }
static void Main(string[] args) { int usedCount = 0; bool[,] matrix = new bool[128, 128]; for (int i = 0; i < matrix.GetLength(0); i++) { string hashInput = $"{Input}-{i}"; string hash = new KnotHash(hashInput).Hash; string[] hashBinary = hash.ToCharArray().Select(x => Convert.ToString(Convert.ToInt32(x.ToString(), 16), 2).PadLeft(4, '0')).ToArray(); char[] hexHash = String.Join("", hashBinary).ToCharArray(); for (int j = 0; j < hexHash.Length; j++) { if (hexHash[j] == '1') { usedCount++; } matrix[i, j] = hexHash[j] == '1'; } } int groups = 0; int?[,] seenMatrix = new int?[128, 128]; for (int i = 0; i < matrix.GetLength(0); i++) { for (int j = 0; j < matrix.GetLength(1); j++) { if (matrix[i, j] && !seenMatrix[i, j].HasValue) { DepthFirstSearch(matrix, seenMatrix, i, j, groups + 1); groups++; } } } Console.WriteLine(groups); Console.ReadLine(); }
static void PartOne(string input) { //for (int j = 0; j < 8; j++) //{ // var ba = KnotHash.HashAsBoolArr(input + "-" + j); // for (int i = 0; i < 8; i++) // { // Console.Write(ba[i] ? '#' : '.'); // } // Console.WriteLine(); //} int numUsed = 0; for (int j = 0; j < 128; j++) { var ba = KnotHash.HashAsBoolArr(input + "-" + j); // Count number of trues, and add to total numUsed += ba.Count(b => b); } Console.WriteLine(numUsed + " squares are used"); }