Esempio n. 1
0
        static void Process4dCycles(string[] _input_grid, _4d_cube_container _container, int _cycles)
        {
            _container.SetBoundaries(0, 0, 0, 0, _input_grid.Length - 1, _input_grid[0].Length - 1, 0, 0);

            for (var x = 0; x < _input_grid.Length; x++)
            {
                for (var y = 0; y < _input_grid[x].Length; y++)
                {
                    _container[x, y, 0, 0] = new cube(_input_grid[x][y] == '#' ? true : false);
                }
            }

            for (var cycle = 0; cycle < _cycles; cycle++)
            {
                // add a 4d layer around the existing hypercubes
                _container.Expand();

                // transform the cubes according to these rules:
                // 1. if a cube is active and exactly 2 or 3 of its neighbors are also active, the cube remains active. Otherwise, the cube becomes inactive.
                //
                // 2. if a cube is inactive but exactly 3 of its neighbors are active, the cube becomes active. Otherwise, the cube remains inactive.
                for (var x = _container.min_x; x <= _container.max_x; x++)
                {
                    for (var y = _container.min_y; y <= _container.max_y; y++)
                    {
                        for (var z = _container.min_z; z <= _container.max_z; z++)
                        {
                            for (var w = _container.min_w; w <= _container.max_w; w++)
                            {
                                if (_container[x, y, z, w].state == true)
                                {
                                    if (_container[x, y, z, w].active_neighbors != 2 && _container[x, y, z, w].active_neighbors != 3)
                                    {
                                        _container[x, y, z, w].Transform(false);
                                    }
                                }
                                else if (_container[x, y, z, w].active_neighbors == 3)
                                {
                                    _container[x, y, z, w].Transform(true);
                                }
                            }
                        }
                    }
                }

                // apply transforms to each cube
                _container.Update();
            }
        }
Esempio n. 2
0
        static async Task Main(string[] args)
        {
            var input_grid = await File.ReadAllLinesAsync("input.txt", Encoding.UTF8);

            var _3d_container = new _3d_cube_container();

            Process3dCycles(input_grid, _3d_container, 6);

            Console.WriteLine(_3d_container.active_cube_count);

            var _4d_container = new _4d_cube_container();

            Process4dCycles(input_grid, _4d_container, 6);

            Console.WriteLine(_4d_container.active_cube_count);
        }