예제 #1
0
 public RecursiveGrid(int depth, RecursiveGrid innerGrid)
 {
     RecursiveGridInternal(null);
     InnerGrid           = innerGrid;
     InnerGrid.OuterGrid = this;
     Depth = depth;
 }
예제 #2
0
        public void Part2()
        {
            // other than LoadData & this method, all of part2 logic is in the RecursiveGrid class
            LoadData();

            // start with the loaded grid, an inner, and an outer grid
            // kind of a clutzy startup, but heck
            RecursiveGrid innerinner = new RecursiveGrid(1, RecursiveGrid.InitializeGrid(new char[5, 5]));
            RecursiveGrid rgridInner = new RecursiveGrid(0, _grid1);

            rgridInner.InnerGrid = innerinner;
            innerinner.OuterGrid = rgridInner;
            RecursiveGrid rgrid = new RecursiveGrid(-1, rgridInner);
            RecursiveGrid tmp;

            for (int i = 0; i < 200; i++)
            {
                bool needOuter = rgrid.ApplyRules();
                tmp = rgrid;
                while (tmp != null)
                {
                    tmp.SwitchSrcAndDst();
                    tmp = tmp.InnerGrid;
                }
                if (needOuter)
                {
                    rgrid = new RecursiveGrid(rgrid.Depth - 1, rgrid);
                }
            }

            int bugCount = 0;

            tmp = rgrid;
            while (tmp != null)
            {
                bugCount += tmp.CountBugs();
                tmp       = tmp.InnerGrid;
            }

            Console.WriteLine("Part2: {0}", bugCount);
        }
예제 #3
0
        public bool ApplyRules()
        {
            bool needOuter = false;

            for (int y = 0; y < 5; y++)
            {
                for (int x = 0; x < 5; x++)
                {
                    int adjacentCount = 0;


                    if (InnerGrid != null)
                    {
                        if (x == 2)
                        {
                            if (y == 1)
                            {
                                adjacentCount += InnerGrid.TopEdgeCount(Selector.Source);
                            }
                            else if (y == 3)
                            {
                                adjacentCount += InnerGrid.BottomEdgeCount(Selector.Source);
                            }
                        }
                        if (y == 2)
                        {
                            if (x == 1)
                            {
                                adjacentCount += InnerGrid.LeftEdgeCount(Selector.Source);
                            }
                            else if (x == 3)
                            {
                                adjacentCount += InnerGrid.RightEdgeCount(Selector.Source);
                            }
                        }
                    }
                    if (OuterGrid != null)
                    {
                        if (x == 0)
                        {
                            adjacentCount += OuterGrid.CheckForBug(1, 2) ? 1 : 0;
                        }
                        if (x == 4)
                        {
                            adjacentCount += OuterGrid.CheckForBug(3, 2) ? 1 : 0;
                        }
                        if (y == 0)
                        {
                            adjacentCount += OuterGrid.CheckForBug(2, 1) ? 1 : 0;
                        }
                        if (y == 4)
                        {
                            adjacentCount += OuterGrid.CheckForBug(2, 3) ? 1 : 0;
                        }
                    }
                    if (x == 2 && y == 2 && InnerGrid != null)
                    {
                        // recurse in
                        InnerGrid.ApplyRules();
                    }
                    else
                    {
                        if (!(x == 2 && y == 2))
                        {
                            // count adjacent bugs
                            adjacentCount += (x > 0 && _grids[srcIndex][x - 1, y] == '#') ? 1 : 0;
                            adjacentCount += (x < 5 - 1 && _grids[srcIndex][x + 1, y] == '#') ? 1 : 0;
                            adjacentCount += (y > 0 && _grids[srcIndex][x, y - 1] == '#') ? 1 : 0;
                            adjacentCount += (y < 5 - 1 && _grids[srcIndex][x, y + 1] == '#') ? 1 : 0;
                        }
                    }

                    char newState = _grids[srcIndex][x, y];

                    if (_grids[srcIndex][x, y] == '#')
                    {
                        if (adjacentCount != 1)
                        {
                            newState = '.';
                        }
                    }
                    else
                    {
                        if (adjacentCount == 1 || adjacentCount == 2)
                        {
                            newState = '#';
                        }
                    }

                    _grids[dstIndex][x, y] = newState;
                }
            }

            if (CountAroundInnerSpot(_grids[dstIndex]) > 0 && InnerGrid == null)
            {
                // allocate a new inner grid
                InnerGrid           = new RecursiveGrid(Depth + 1, InitializeGrid(new char[5, 5])); // dumb - need an empty constructor
                InnerGrid.OuterGrid = this;
            }

            // see if we need to grow outward - if so, return true & outer control will allocate a new one
            needOuter = EdgeCount(Selector.Destination) > 0;

            return(needOuter && OuterGrid == null);
        }