Пример #1
0
            public List <Tile> AdjacentTiles(Tile tile, bool addIfMissing = true)
            {
                // directions of neighbour
                List <(int x, int y)> neighboursIndex = new List <(int x, int y)>()
                {
                    (1, 0), (1, -1), (0, -1), (-1, 0), (-1, 1), (0, 1)
                };

                // e = y: same, x: +1
                // se = y: -1, x: +1
                // sw = y: -1, x: same
                // w = y: same, x: -1
                // nw = y: 1, x: -1
                // ne = y: 1, x: same

                var neighbours = new List <Tile>();

                foreach (var item in neighboursIndex)
                {
                    int x = item.x + tile.x;
                    int y = item.y + tile.y;

                    if (TilesVisited.ContainsKey($"{x},{y}"))
                    {
                        neighbours.Add(TilesVisited[$"{x},{y}"]);
                    }
                    else if (!NewTiles.ContainsKey($"{x},{y}") && addIfMissing)
                    {
                        NewTiles.Add($"{x},{y}", new Tile(x, y));
                    }
                }

                return(neighbours);
            }
Пример #2
0
            public void Run2(int days = 10)
            {
                Run();

                // start to execute days of flipping
                for (int day = 1; day <= days; day++)
                {
                    // Any black tile with zero or more than 2 black tiles immediately adjacent to it is flipped to white.
                    // Any white tile with exactly 2 black tiles immediately adjacent to it is flipped to black.
                    foreach (var(ind, tile) in TilesVisited)
                    {
                        var adjacent      = AdjacentTiles(tile);
                        var blackAdjacent = adjacent.Where((item) => !item.White).Count();

                        if (tile.White && blackAdjacent == 2)
                        {
                            // exactly 2 black tiles, flip to black!
                            tile.FlipTo = false;
                        }
                        else if (!tile.White && (blackAdjacent == 0 || blackAdjacent > 2))
                        {
                            tile.FlipTo = true;
                        }
                    }

                    foreach (var(ind, tile) in NewTiles)
                    {
                        TilesVisited.Add(tile.Index, tile);

                        var adjacent      = AdjacentTiles(tile, false);
                        var blackAdjacent = adjacent.Where((item) => !item.White).Count();

                        if (tile.White && blackAdjacent == 2)
                        {
                            // exactly 2 black tiles, flip to black!
                            tile.FlipTo = false;
                        }
                        else if (!tile.White && (blackAdjacent == 0 || blackAdjacent > 2))
                        {
                            tile.FlipTo = true;
                        }
                    }

                    foreach (var(ind, tile) in TilesVisited)
                    {
                        tile.ExecuteFlip();
                    }

                    NewTiles = new Dictionary <string, Tile>();
                    var nr = CountBlackTiles();

                    // Console.WriteLine($"Day {day}: {nr}");
                }
            }
Пример #3
0
            public void Run()
            {
                // convert the steps to a x and y position
                // https://gamedev.stackexchange.com/a/44814
                foreach (var tilepath in TilesToFlip)
                {
                    int x          = 0;
                    int y          = 0;
                    var pathToTile = tilepath;

                    while (pathToTile.Length != 0)
                    {
                        var indexToRemove = 1;
                        if (pathToTile.StartsWith("e"))
                        {
                            // e = y: same, x: +1
                            x++;
                        }
                        else if (pathToTile.StartsWith("se"))
                        {
                            // se = y: -1, x: +1
                            y--;
                            x++;
                            indexToRemove = 2;
                        }
                        else if (pathToTile.StartsWith("sw"))
                        {
                            // sw = y: -1, x: same
                            y--;
                            indexToRemove = 2;
                        }
                        else if (pathToTile.StartsWith("w"))
                        {
                            // w = y: same, x: -1
                            x--;
                        }
                        else if (pathToTile.StartsWith("nw"))
                        {
                            // nw = y: 1, x: -1
                            y++;
                            x--;
                            indexToRemove = 2;
                        }
                        else if (pathToTile.StartsWith("ne"))
                        {
                            // ne = y: 1, x: same
                            y++;
                            indexToRemove = 2;
                        }

                        pathToTile = pathToTile.Substring(indexToRemove);
                    }

                    // tile to flip!
                    var tile = new Tile(x, y);
                    if (!TilesVisited.ContainsKey(tile.Index))
                    {
                        TilesVisited[tile.Index] = tile;
                    }

                    var visitedTile = TilesVisited[tile.Index];
                    visitedTile.Flip();
                }
            }
Пример #4
0
            public int CountBlackTiles()
            {
                int sum = TilesVisited.Where((item) => !item.Value.White).Count();

                return(sum);
            }