private static int CalculateActiveNeighboursB(Coordinate4D coordinate, HashSet <Coordinate4D> activeCubes) { int count = 0; for (int dx = -1; dx < 2; dx++) { for (int dy = -1; dy < 2; dy++) { for (int dz = -1; dz < 2; dz++) { for (int dw = -1; dw < 2; dw++) { if (dx == 0 && dy == 0 && dz == 0 && dw == 0) { continue; } if (activeCubes.Contains(new Coordinate4D(coordinate.X + dx, coordinate.Y + dy, coordinate.Z + dz, coordinate.W + dw))) { count++; } } } } } return(count); }
private IEnumerable <bool> GetNeighbors(Coordinate4D cord) { var values = new List <bool>(); for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { for (int z = -1; z <= 1; z++) { for (int w = -1; w <= 1; w++) { var coordinate = new Coordinate4D { X = cord.X + x, Y = cord.Y + y, Z = cord.Z + z, W = cord.W + w }; if (!coordinate.Equals(cord)) { values.Add(GetCube(coordinate)); } } } } } return(values); }
static void RunB(string filePath) { var lines = File.ReadAllLines(filePath); var activeCubes = new HashSet <Coordinate4D>(); for (int y = 0; y < lines.Length; y++) { for (int x = 0; x < lines[y].Length; x++) { if (lines[y][x] == '#') { activeCubes.Add(new Coordinate4D(x, y, 0, 0)); } } } var next = new HashSet <Coordinate4D>(); int cycles = 6; var visited = new HashSet <Coordinate4D>(); for (int i = 1; i <= cycles; i++) { foreach (var c in activeCubes) { for (int dx = -1; dx < 2; dx++) { for (int dy = -1; dy < 2; dy++) { for (int dz = -1; dz < 2; dz++) { for (int dw = -1; dw < 2; dw++) { Coordinate4D current = new Coordinate4D(c.X + dx, c.Y + dy, c.Z + dz, c.W + dw); if (visited.Contains(current)) { continue; } visited.Add(current); int activeNeightbours = CalculateActiveNeighboursB(current, activeCubes); bool currentlyActive = activeCubes.Contains(current); if (activeNeightbours == 3 && !currentlyActive) { next.Add(current); } else if ((activeNeightbours == 2 || activeNeightbours == 3) && currentlyActive) { next.Add(current); } } } } } } activeCubes = next.ToHashSet(); next = new HashSet <Coordinate4D>(); visited.Clear(); } Console.WriteLine($"count = {activeCubes.Count}"); }
private bool GetCube(Coordinate4D coordinate) { bool active; if (Cubes.TryGetValue(coordinate, out active)) { return(active); } if (!NextCubes.TryGetValue(coordinate, out _)) { NextCubes.Add(coordinate, false); } return(false); }