static void ProcessCycles(DimensionSet dimension, int cycles, bool is4D) { for (int i = 1; i <= cycles; i++) { Dictionary <DimensionLocation, bool> newValues = new(); (DimensionLocation min, DimensionLocation max) = dimension.GetBounds(); for (int w = min.W - 1; w <= max.W + 1; w++) { if (!is4D && w != 0) { continue; } for (int z = min.Z - 1; z <= max.Z + 1; z++) { for (int y = min.Y - 1; y <= max.Y + 1; y++) { for (int x = min.X - 1; x <= max.X + 1; x++) { var location = new DimensionLocation(x, y, z, w); bool active = dimension.IsActive(location); var neighbors = location.GetNeighbors(is4D); var activeNeighborCount = neighbors .Where(x => dimension.IsActive(x)) .Count(); if (active) { newValues[location] = activeNeighborCount is 2 or 3; } else { newValues[location] = activeNeighborCount == 3; } } } } } foreach (KeyValuePair <DimensionLocation, bool> item in newValues) { dimension[item.Key] = item.Value; } Debug.WriteLine($"Cycle: {i}"); Debug.WriteLine(""); Debug.WriteLine(dimension); Debug.WriteLine(""); } }
static void Main(string[] args) { DimensionSet dimension = ParseDimension3D("input.txt"); Stopwatch watch = new Stopwatch(); watch.Start(); ProcessCycles(dimension, 6, is4D: false); int activeCount6Cycles3D = dimension.Where(x => x.Value).Count(); watch.Stop(); Console.WriteLine($"Active count after 6 cycles in 3D: {activeCount6Cycles3D} - Elapsed: {watch.Elapsed}"); watch.Reset(); dimension = ParseDimension3D("input.txt"); watch.Start(); ProcessCycles(dimension, 6, is4D: true); int activeCount6Cycles4D = dimension.Where(x => x.Value).Count(); watch.Stop(); Console.WriteLine($"Active count after 6 cycles in 4D: {activeCount6Cycles4D} - Elapsed: {watch.Elapsed}"); }