コード例 #1
0
ファイル: 17dec.cs プロジェクト: lmellblom/advent-of-code
            public override int ActiveNeighbors(Cube cube, bool addIfMissing = true)
            {
                var neighbours = 0;

                int xIndex = cube.x;
                int yIndex = cube.y;
                int zIndex = cube.z;
                int wIndex = (cube as Cube4D).w;

                // -1, 0, 1
                List <int> xs = Enumerable.Range(-1, 3).Select(nr => nr + xIndex).ToList();
                List <int> ys = Enumerable.Range(-1, 3).Select(nr => nr + yIndex).ToList();
                List <int> zs = Enumerable.Range(-1, 3).Select(nr => nr + zIndex).ToList();
                List <int> ws = Enumerable.Range(-1, 3).Select(nr => nr + wIndex).ToList();

                foreach (var x in xs)
                {
                    foreach (var y in ys)
                    {
                        foreach (var z in zs)
                        {
                            foreach (var w in ws)
                            {
                                if (x == xIndex && y == yIndex && z == zIndex && w == wIndex)
                                {
                                    // this is the cube we have
                                    continue;
                                }

                                var index = $"{x},{y},{z},{w}";
                                if (Cubes.ContainsKey(index))
                                {
                                    var neighbour = Cubes[index];
                                    if (neighbour.IsActive)
                                    {
                                        neighbours++;
                                    }
                                }
                                else if (!NewCubes.ContainsKey(index) && addIfMissing)
                                {
                                    var newCube = new Cube4D(x, y, z, w);
                                    newCube.IsActive = false;
                                    NewCubes.Add(newCube.Index, newCube);
                                }
                            }
                        }
                    }
                }

                return(neighbours);
            }
コード例 #2
0
 public Cube Neighbour(Position position) => Cubes.ContainsKey(position.ToString()) ? Cubes[position.ToString()] : null;
コード例 #3
0
 public IEnumerable <Cube> Neighbours() => Position.Neighbours().Select(n => Cubes.ContainsKey(n.ToString()) ? Cubes[n.ToString()] : null);