コード例 #1
0
ファイル: 24dec.cs プロジェクト: lmellblom/advent-of-code
            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
ファイル: 24dec.cs プロジェクト: lmellblom/advent-of-code
            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();
                }
            }