コード例 #1
0
        private int GetNeighboursPart2(Vector2Int pos)
        {
            RectangleInt bounds = new RectangleInt(Vector2Int.Zero, new Vector2Int(width - 1, height - 1));
            int          amount = 0;

            foreach (Vector2Int offset in offsets)
            {
                Vector2Int accumulator = new Vector2Int(pos.X, pos.Y) + offset;

                while (bounds.IsInRectangle(accumulator))
                {
                    if (tiles[accumulator.Y][accumulator.X].IsSeat)
                    {
                        if (tiles[accumulator.Y][accumulator.X].Occupied)
                        {
                            amount++;
                        }

                        break;
                    }

                    accumulator += offset;
                }
            }

            return(amount);
        }
コード例 #2
0
ファイル: Day24.cs プロジェクト: w200338/Advent-of-Code-2019
        /// <inheritdoc />
        public string Part1()
        {
            List <int> bioRatings = new List <int>();

            bioRatings.Add(CalcScore(inputGrid));
            RectangleInt bounds = new RectangleInt(Vector2Int.Zero, new Vector2Int(4, 4));

            // do a step
            while (true)
            {
                List <List <char> > newGrid = new List <List <char> >();

                for (int i = 0; i < inputGrid.Count; i++)
                {
                    newGrid.Add(new List <char>());

                    for (int j = 0; j < inputGrid[0].Count; j++)
                    {
                        // check neighbours
                        int neighbourCount = 0;
                        foreach (Vector2Int neighbour in Around(new Vector2Int(j, i)))
                        {
                            if (bounds.IsInRectangle(neighbour))
                            {
                                if (inputGrid[neighbour.Y][neighbour.X] == '#')
                                {
                                    neighbourCount++;
                                }
                            }
                        }

                        // add or remove creature
                        if (inputGrid[i][j] == '#')
                        {
                            newGrid[i].Add(neighbourCount == 1 ? '#' : '.');
                        }
                        else
                        {
                            if (neighbourCount == 1 || neighbourCount == 2)
                            {
                                newGrid[i].Add('#');
                            }
                            else
                            {
                                newGrid[i].Add('.');
                            }
                        }

                        //newGrid[i].Add()
                    }
                }

                // print board

                // calc bio rating
                int bioRating = CalcScore(newGrid);

                // check if it already existed
                if (bioRatings.Contains(bioRating))
                {
                    return(bioRating.ToString());
                }

                bioRatings.Add(bioRating);

                inputGrid = newGrid;
            }

            return($"");
        }
コード例 #3
0
ファイル: Day24.cs プロジェクト: w200338/Advent-of-Code-2019
        private int CountNeighbours(List <GridDepth> grids, Vector3Int position)
        {
            int output = 0;

            RectangleInt bounds = new RectangleInt(Vector2Int.Zero, new Vector2Int(4, 4));
            Vector2Int   pos2D  = new Vector2Int(position.X, position.Y);

            // check direct neighbours
            foreach (Vector2Int neighbour in Around(pos2D))
            {
                if (neighbour != new Vector2Int(2, 2) && bounds.IsInRectangle(neighbour))
                {
                    if (grids.FirstOrDefault(g => g.Depth == position.Z)?.Grid[neighbour.Y][neighbour.X] == '#')
                    //if (grids[depthIndex].Grid[neighbour.Y][neighbour.X] == '#')
                    {
                        output++;
                    }
                }
            }

            // check depth below
            if (pos2D == new Vector2Int(2, 1))
            {
                for (int i = 0; i < 5; i++)
                {
                    output += grids.FirstOrDefault(g => g.Depth == position.Z + 1)?.Grid[0][i] == '#' ? 1 : 0;
                    //output += grids[depthIndex + 1]?.Grid[0][i] == '#' ? 1 : 0;
                }
            }
            else if (pos2D == new Vector2Int(1, 2))
            {
                for (int i = 0; i < 5; i++)
                {
                    output += grids.FirstOrDefault(g => g.Depth == position.Z + 1)?.Grid[i][0] == '#' ? 1 : 0;
                    //output += grids[depthIndex + 1].Grid[i][0] == '#' ? 1 : 0;
                }
            }
            else if (pos2D == new Vector2Int(3, 2))
            {
                for (int i = 0; i < 5; i++)
                {
                    output += grids.FirstOrDefault(g => g.Depth == position.Z + 1)?.Grid[i][4] == '#' ? 1 : 0;
                    //output += grids[depthIndex + 1].Grid[i][4] == '#' ? 1 : 0;
                }
            }
            else if (pos2D == new Vector2Int(2, 3))
            {
                for (int i = 0; i < 5; i++)
                {
                    output += grids.FirstOrDefault(g => g.Depth == position.Z + 1)?.Grid[4][i] == '#' ? 1 : 0;
                    //output += grids[depthIndex + 1].Grid[4][i] == '#' ? 1 : 0;
                }
            }

            // check depth above
            if (position.X == 0)
            {
                output += grids.FirstOrDefault(g => g.Depth == position.Z - 1)?.Grid[2][1] == '#' ? 1 : 0;
                //output += grids[depthIndex - 1].Grid[2][1] == '#' ? 1 : 0;
            }
            else if (position.X == 4)
            {
                output += grids.FirstOrDefault(g => g.Depth == position.Z - 1)?.Grid[2][3] == '#' ? 1 : 0;
                //output += grids[depthIndex - 1].Grid[2][3] == '#' ? 1 : 0;
            }

            if (position.Y == 0)
            {
                output += grids.FirstOrDefault(g => g.Depth == position.Z - 1)?.Grid[1][2] == '#' ? 1 : 0;
                //output += grids[depthIndex - 1].Grid[1][2] == '#' ? 1 : 0;
            }
            else if (position.Y == 4)
            {
                output += grids.FirstOrDefault(g => g.Depth == position.Z - 1)?.Grid[3][2] == '#' ? 1 : 0;
                //output += grids[depthIndex - 1].Grid[3][2] == '#' ? 1 : 0;
            }

            return(output);
        }
コード例 #4
0
        private List <PortalPath> GeneratePairs(Portal startPortal, List <Portal> allPortals)
        {
            RectangleInt bounds = new RectangleInt(Vector2Int.Zero, new Vector2Int(inputGrid[0].Count - 1, inputGrid.Count - 1));
            //Vector2Int startLocation = startPortal.PositionA;

            //List<PortalPair> output = new List<PortalPair>();
            List <PortalPath>    output           = new List <PortalPath>();
            HashSet <Vector2Int> visitedLocations = new HashSet <Vector2Int>();

            Queue <Vector2Int> locationsLeft = new Queue <Vector2Int>();
            Queue <int>        distances     = new Queue <int>();

            locationsLeft.Enqueue(startPortal.Position);
            //locationsLeft.Enqueue(startPortal.PositionB);
            distances.Enqueue(0);
            //distances.Enqueue(0);

            while (locationsLeft.Count > 0)
            {
                // get current position from queue
                Vector2Int currentPos = locationsLeft.Dequeue();
                int        distance   = distances.Dequeue();

                // skip over an already visited location
                if (visitedLocations.Contains(currentPos))
                {
                    continue;
                }

                visitedLocations.Add(currentPos);

                char currentTile = inputGrid[currentPos.Y][currentPos.X];

                // teleporter
                if (char.IsUpper(currentTile))
                {
                    Portal            otherPortal = null;// = allPortals.FirstOrDefault(p => p.Position.Equals(currentPos));
                    List <Vector2Int> around      = Around(currentPos);
                    foreach (var pos in around)
                    {
                        //if (allPortals.FirstOrDefault(p => p.Position.Equals(pos)) != null)
                        if (inputGrid[pos.Y][pos.X] == '.')
                        {
                            otherPortal = allPortals.FirstOrDefault(p => p.Position.Equals(pos));

                            if (otherPortal != null && !otherPortal.Equals(startPortal) && otherPortal.Name != "AA")
                            {
                                output.Add(new PortalPath()
                                {
                                    OtherPortal = otherPortal,
                                    Distance    = distance - 1
                                });

                                break;
                            }
                        }
                    }

                    //Portal otherPortal = allPortals.FirstOrDefault(p => p.PositionA.Equals(currentPos) || p.PositionB.Equals(currentPos));
                }
                // open space
                else if (currentTile == '.')
                {
                    List <Vector2Int> around = Around(currentPos);
                    foreach (Vector2Int aroundPos in around)
                    {
                        if (bounds.IsInRectangle(aroundPos))
                        {
                            locationsLeft.Enqueue(aroundPos);
                            distances.Enqueue(distance + 1);
                        }
                    }
                }
            }

            return(output);
        }