示例#1
0
文件: Day24.cs 项目: Pusiu/AoC_2020
        public override void Solve()
        {
            StreamReader  sr    = new StreamReader("Inputs/day24.txt");
            List <string> lines = new List <string>();

            Console.OutputEncoding = System.Text.Encoding.UTF8;

            HexTile referenceTile = new HexTile(0, 0);

            tilemap.Add(new Tuple <int, int>(referenceTile.x, referenceTile.y), referenceTile);

            while (!sr.EndOfStream)
            {
                string line = sr.ReadLine();
                lines.Add(line);
                Match   m;
                HexTile currentTile = referenceTile;
                do
                {
                    m = Regex.Match(line, @"(se)|(ne)|(sw)|(nw)|(e)|(w)");
                    if (m.Success)
                    {
                        HexTile.Direction dir = (HexTile.Direction)Enum.Parse(typeof(HexTile.Direction), m.Value.ToUpper());
                        int x = currentTile.x;
                        int y = currentTile.y;
                        switch (dir)
                        {
                        case HexTile.Direction.NE:
                            x++;
                            y--;
                            break;

                        case HexTile.Direction.E:
                            x++;
                            break;

                        case HexTile.Direction.SE:
                            y++;
                            break;

                        case HexTile.Direction.NW:
                            y--;
                            break;

                        case HexTile.Direction.W:
                            x--;
                            break;

                        case HexTile.Direction.SW:
                            x--;
                            y++;
                            break;
                        }
                        var pos = new Tuple <int, int>(x, y);
                        if (!tilemap.ContainsKey(pos))
                        {
                            HexTile t = new HexTile(pos.Item1, pos.Item2);
                            tilemap.Add(pos, t);
                        }

                        currentTile = tilemap[pos];
                    }
                    line = line.Substring(m.Value.Length);
                } while (m.Success);
                currentTile.isBlack = !currentTile.isBlack;
            }

            Console.WriteLine($"Part 1: {tilemap.Values.Count(x => x.isBlack)}");

            for (int i = 1; i <= 100; i++)
            {
                HashSet <Tuple <int, int> > tilesToFill = new HashSet <Tuple <int, int> >();
                foreach (var k in tilemap.Keys.ToList())
                {
                    foreach (var d in adjecentDirs)
                    {
                        var pos = new Tuple <int, int>(k.Item1 + d.Item1, k.Item2 + d.Item2);
                        if (!tilemap.ContainsKey(pos))
                        {
                            tilesToFill.Add(pos);
                        }
                    }
                }
                foreach (var t in tilesToFill)
                {
                    tilemap.Add(t, new HexTile(t.Item1, t.Item2));
                }

                var toFlip = new List <Tuple <int, int> >();

                foreach (var k in tilemap.Keys)
                {
                    int c = GetAdjecentBlackTileCount(tilemap[k]);
                    if (tilemap[k].isBlack)
                    {
                        if (c == 0 || c > 2)
                        {
                            toFlip.Add(k);
                        }
                    }
                    else
                    {
                        if (c == 2)
                        {
                            toFlip.Add(k);
                        }
                    }
                }

                foreach (var k in toFlip)
                {
                    tilemap[k].isBlack = !tilemap[k].isBlack;
                }

                Console.WriteLine($"Day {i}: {tilemap.Values.Count(x => x.isBlack)}");
            }
            Console.WriteLine($"Part 2: {tilemap.Values.Count(x => x.isBlack)}");
        }
示例#2
0
文件: Day24.cs 项目: Pusiu/AoC_2020
 public HexTile(HexTile h)
 {
     x       = h.x;
     y       = h.y;
     isBlack = h.isBlack;
 }