Exemplo n.º 1
0
 private bool SeatExistsInGrid(int column, int row)
 {
     return(column >= 0 &&
            column < SeatGrid.GetLongLength(0) &&
            row >= 0 &&
            row < SeatGrid.GetLongLength(1));
 }
Exemplo n.º 2
0
            public bool Equals(SeatGrid other)
            {
                if (Grid.GetLength(0) != other.Grid.GetLength(0))
                {
                    return(false);
                }

                if (Grid.GetLength(1) != other.Grid.GetLength(1))
                {
                    return(false);
                }

                for (int y = 0; y < Grid.GetLength(1); y++)
                {
                    for (int x = 0; x < Grid.GetLength(0); x++)
                    {
                        if (Grid[x, y] != other.Grid[x, y])
                        {
                            return(false);
                        }
                    }
                }

                return(true);
            }
Exemplo n.º 3
0
        static SeatGrid LoadSeatingGrid(string filename)
        {
            var reader = default(StreamReader);
            var result = new SeatGrid();

            try
            {
                using (reader = new StreamReader(filename))
                {
                    var input = reader.ReadToEnd().Split(Environment.NewLine);
                    result.Grid = new char[input[0].Length, input.Length];

                    for (int y = 0; y < input.Length; y++)
                    {
                        for (int c = 0; c < input[0].Length; c++)
                        {
                            result.Grid[c, y] = input[y][c];
                        }
                    }

                    return(result);
                }
            }
            finally
            {
                reader?.Close();
            }
        }
Exemplo n.º 4
0
        public void Minimal_Grid_Book()
        {
            // Arrange
            var s = new SeatGrid(2, 2, 1, new AlphabeticalGenerator(), new NumericalGenerator());

            // Act
            var seats = s.Book(3);

            // Assert
            Assert.Equal(new string[] { "1", "0", "1" }, seats.Select((x) => x.ID).ToArray());
        }
Exemplo n.º 5
0
        public void Overbook()
        {
            // Arrange
            int n = 3;
            int m = 5;
            var s = new SeatGrid(n, m, m / 2, new AlphabeticalGenerator(), new NumericalGenerator());

            // Act
            var seats = s.Book(n * m + 100);

            // Assert
            Assert.Equal(n * m, seats.Length);
        }
Exemplo n.º 6
0
        public void Book_All()
        {
            // Arrange
            int n = 3;
            int m = 5;
            var s = new SeatGrid(n, m, m / 2, new AlphabeticalGenerator(), new NumericalGenerator());

            // Act
            s.Book(n * m);

            // Assert
            Assert.Equal(n * m, s.Rows.Select((r) => r.Seats.Count((t) => t.Booked)).Sum());
        }
Exemplo n.º 7
0
        public void Book_One()
        {
            // Arrange
            int n = 3;
            int m = 5;
            var s = new SeatGrid(n, m, m / 2, new AlphabeticalGenerator(), new NumericalGenerator());

            // Act
            s.Book(1);

            // Assert
            Assert.Equal(1, s.Rows[0].Seats.Select(x => x.Booked).Count((x) => x));
        }
Exemplo n.º 8
0
        public int GetEquilibriumOccupiedSeatCount()
        {
            int occupiedSeatCount = 0;

            for (int row = 0; row < SeatGrid.GetLongLength(0); row++)
            {
                for (int column = 0; column < SeatGrid.GetLongLength(1); column++)
                {
                    if (SeatGrid[row, column].CurrentSeatType == '#')
                    {
                        occupiedSeatCount++;
                    }
                }
            }

            return(occupiedSeatCount);
        }
Exemplo n.º 9
0
            public SeatGrid Iterate(bool immediate, int tolerance)
            {
                SeatGrid result = new SeatGrid();

                result.Grid = new char[Grid.GetLength(0), Grid.GetLength(1)];

                // Iterate over every seat
                for (int y = 0; y < Grid.GetLength(1); y++)
                {
                    for (int x = 0; x < Grid.GetLength(0); x++)
                    {
                        // Count occupied seats
                        var total = 0;

                        total += (TryGetSeat(immediate, x, y, -1, -1, out var ch) && ch == '#') ? 1 : 0;    // Top left
                        total += (TryGetSeat(immediate, x, y, 0, -1, out ch) && ch == '#') ? 1 : 0;         // Top
                        total += (TryGetSeat(immediate, x, y, 1, -1, out ch) && ch == '#') ? 1 : 0;         // Top right

                        total += (TryGetSeat(immediate, x, y, -1, 0, out ch) && ch == '#') ? 1 : 0;         // Left
                        total += (TryGetSeat(immediate, x, y, 1, 0, out ch) && ch == '#') ? 1 : 0;          // Right

                        total += (TryGetSeat(immediate, x, y, -1, 1, out ch) && ch == '#') ? 1 : 0;         // Bottom left
                        total += (TryGetSeat(immediate, x, y, 0, 1, out ch) && ch == '#') ? 1 : 0;          // Bottom
                        total += (TryGetSeat(immediate, x, y, 1, 1, out ch) && ch == '#') ? 1 : 0;          // Bottom right

                        // Copy the value over to the target...
                        result.Grid[x, y] = Grid[x, y];

                        // ...and deicde if it's going to mutate

                        // Is the seat empty and no adjacent are occupied?
                        if (Grid[x, y] == 'L' && total == 0)
                        {
                            result.Grid[x, y] = '#';
                        }
                        // Is the seat occupied and needs to be emptied?
                        else if (Grid[x, y] == '#' && total >= tolerance)
                        {
                            result.Grid[x, y] = 'L';
                        }
                    }
                }

                return(result);
            }
Exemplo n.º 10
0
        /// <summary>
        /// Generate a blank seating grid with seat labels generated
        /// </summary>
        /// <returns>a generated SeatGrid representing with labeled seats for the grid</returns>
        SeatGrid GenerateSeatGrid(int seatRows, int seatPerRow)
        {
            string[] rowLabels = _rowLabelcsv.Split(',');

            int      seatNumStartAt = 1;
            SeatGrid newSeatGrid    = new SeatGrid();

            for (int r = 0; r < seatRows; r++)
            {
                for (int s = seatNumStartAt; s < seatPerRow + 1; s++)
                {
                    newSeatGrid.Seats.Add(new Seat {
                        SeatLabel = $"{rowLabels[r]}{s}"
                    });
                }
            }
            return(newSeatGrid);
        }
Exemplo n.º 11
0
        public void Should_Be_37_Full_Seats()
        {
            var input = new string[] {
                "L.LL.LL.LL",
                "LLLLLLL.LL",
                "L.L.L..L..",
                "LLLL.LL.LL",
                "L.LL.LL.LL",
                "L.LLLLL.LL",
                "..L.L.....",
                "LLLLLLLLLL",
                "L.LLLLLL.L",
                "L.LLLLL.LL"
            };
            var seatGrid = new SeatGrid(input);

            seatGrid.RunAllRounds();

            seatGrid.CountFullSeats(seatGrid.Grids.Last()).ShouldBe(37);
        }
Exemplo n.º 12
0
        public override int SolvePart1()
        {
            var grid = new SeatGrid(baseGrid);

            bool gridAdjusted;

            do
            {
                gridAdjusted = false;

                var tempGrid = new SeatGrid(grid);

                for (int x = 0; x < grid.Width; x++)
                {
                    for (int y = 0; y < grid.Height; y++)
                    {
                        var cell = grid[x, y];

                        if (cell == SeatGridCell.Floor)
                        {
                            continue;
                        }

                        var adjacent = grid.GetNeighborValues(x, y, SeatGridCell.Occupied);

                        tempGrid[x, y] = (cell, adjacent) switch
                        {
                            (SeatGridCell.Occupied, >= 4) => SeatGridCell.Vacant,
                            (SeatGridCell.Vacant, 0) => SeatGridCell.Occupied,
                            _ => cell,
                        };

                        if (tempGrid[x, y] != grid[x, y])
                        {
                            gridAdjusted = true;
                        }
                    }
                }

                grid = tempGrid;
            }while (gridAdjusted);
Exemplo n.º 13
0
        static SeatGrid GenerateSeatGrid(int seatRows, int seatPerRow)
        {
            string[] rowLabels = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z".Split(',');

            int      seatNumStartAt = 1;
            SeatGrid newSeatGrid    = new SeatGrid()
            {
                Id = seatGridId++
            };

            for (int r = 0; r < seatRows; r++)
            {
                for (int s = seatNumStartAt; s < seatPerRow + 1; s++)
                {
                    newSeatGrid.Seats.Add(new Seat {
                        Id = seatId++, SeatLabel = $"{rowLabels[r]}{s}"
                    });
                }
            }
            return(newSeatGrid);
        }
Exemplo n.º 14
0
 public SeatGrid(SeatGrid other)
     : base(other)
 {
 }