Esempio n. 1
0
    public override Output Part2()
    {
        var input = Input.Lines();
        var grid  = new SparsePlaneGrid2D <int>(input[0].Length, input.Length);

        for (int y = 0; y < input.Length; y++)
        {
            for (int x = 0; x < input[y].Length; x++)
            {
                grid[x, y] = Int32.Parse(input[y][x].ToString()) + 1;
            }
        }

        List <Coordinate2D> flashes = new();
        long answer = 0;
        var  i      = 0;

        while (true)
        {
            flashes.Clear();
            for (int y = 0; y < grid.J.Length; y++)
            {
                for (int x = 0; x < grid.I.Length; x++)
                {
                    Increase(x, y);
                }
            }
            if (flashes.Count == grid.Count())
            {
                return(i + 1);
            }
            i++;
        }

        void Increase(long x, long y)
        {
            if (flashes.Contains(new(x, y)))
            {
                return;
            }

            grid[x, y]++;
            if (grid[x, y] == 11)
            {
                answer++;
                grid[x, y] = 1;
                if (!flashes.Contains(new(x, y)))
                {
                    flashes.Add(new(x, y));
                    var ns = grid.Neighbors(new(x, y));
                    foreach (var n in ns)
                    {
                        Increase(n.X, n.Y);
                    }
                }
            }
        }
    }
Esempio n. 2
0
    public override Output Part2()
    {
        var input     = Input.Paragraphs();
        var algorithm = input[0].Replace("\n", "");

        var imageData = input[1].Lines();
        var grid      = new SparsePlaneGrid2D <bool>();

        for (int y = 0; y < imageData.Length; y++)
        {
            for (int x = 0; x < imageData[y].Length; x++)
            {
                grid[x, y] = imageData[y][x] == '#';
            }
        }

        var x0 = 0;
        var xN = imageData[0].Length;
        var y0 = 0;
        var yN = imageData.Length;

        for (int t = 0; t < 50; t++)
        {
            x0--;
            xN++;
            y0--;
            yN++;
            var newDefault = (grid.DefaultValue ? algorithm.Last() : algorithm.First()) == '#';
            var newGrid    = new SparsePlaneGrid2D <bool>(newDefault);

            for (var y = y0; y <= yN; y++)
            {
                for (var x = x0; x <= xN; x++)
                {
                    var rule = 0;
                    rule = rule * 2 + (grid[x - 1, y - 1] ? 1 : 0);
                    rule = rule * 2 + (grid[x, y - 1] ? 1 : 0);
                    rule = rule * 2 + (grid[x + 1, y - 1] ? 1 : 0);
                    rule = rule * 2 + (grid[x - 1, y] ? 1 : 0);
                    rule = rule * 2 + (grid[x, y] ? 1 : 0);
                    rule = rule * 2 + (grid[x + 1, y] ? 1 : 0);
                    rule = rule * 2 + (grid[x - 1, y + 1] ? 1 : 0);
                    rule = rule * 2 + (grid[x, y + 1] ? 1 : 0);
                    rule = rule * 2 + (grid[x + 1, y + 1] ? 1 : 0);

                    newGrid[x, y] = algorithm[rule] == '#';
                }
            }

            grid = newGrid;
        }

        return(grid.Count(x => x.Value));
    }