Beispiel #1
0
        public static async Task Run()
        {
            if (await Load() is not SeatState[,] seats)
            {
                return;
            }

            int height       = seats.GetLength(0);
            int width        = seats.GetLength(1);
            var updatedSeats = new SeatState[height, width];

            bool changed;

            do
            {
                changed = false;

                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        updatedSeats[i, j] =
                            (
                                Offsets.Count(off => IsOccupiedAlong(seats, j, i, off.x, off.y)),
                                seats[i, j]
                            )
                            switch
                        {
                            (0, SeatState.Empty) => SeatState.Occupied,
                            (>= 5, SeatState.Occupied) => SeatState.Empty,
                            (_, SeatState state) => state
                        };

                        if (updatedSeats[i, j] != seats[i, j])
                        {
                            changed = true;
                        }
                    }
                }

                (seats, updatedSeats) = (updatedSeats, seats);
            } while (changed);

            Console.WriteLine($"Number of occupied seats: {seats.Flatten().Count(s => s == SeatState.Occupied)}");
        }