public void SolvePuzzle2() { var input = new FileReader() .GetResource("AdventOfCode2020.Tests.Day11.PuzzleInput.txt"); var seats = SeatParser.Parse2(input); var seatFinder = new SeatFinder(); Map currentMap = null; var cont = false; int count = 0; do { var map = currentMap?.DeepCopy(); currentMap = seatFinder.Execute2(currentMap ?? seats); if (map == null) { cont = true; } else if (map.Compare(currentMap)) { cont = false; } count++; } while (cont); var spaces = currentMap !.GetAllSpaces().Count(x => x is OccupiedSeat); _testOutputHelper.WriteLine(spaces.ToString()); _testOutputHelper.WriteLine(count.ToString()); }
public void RulesTests(string input, int expectedOccupiedSeats) { var seats = SeatParser .Parse2(input.Replace("|", Environment.NewLine)); var seatFinder = new SeatFinder(); var newMap = seatFinder.Execute(seats); var space = newMap.GetAllSpaces(); Assert.Equal(expectedOccupiedSeats, space.Count(floor => floor is OccupiedSeat)); }
/// <summary> /// Gets the nearest seats for a particular location. /// This can be the neighbouring seats, or visible seats. /// </summary> /// <param name="type">The search type - e.g. nearest or visible.</param> /// <param name="row">The starting row.</param> /// <param name="column">The starting column.</param> /// <returns></returns> public IEnumerable <char> GetNearestSeats(SeatFinder type, int row, int column) { // This is a list containing all valid directions // e.e. up, down, left, right and diagonals. var directions = new List <Tuple <int, int> >() { new Tuple <int, int>(-1, -1), new Tuple <int, int>(-1, 0), new Tuple <int, int>(-1, 1), new Tuple <int, int>(0, -1), new Tuple <int, int>(0, 1), new Tuple <int, int>(1, -1), new Tuple <int, int>(1, 0), new Tuple <int, int>(1, 1) }; foreach (var direction in directions) { int posX = row + direction.Item1; int posY = column + direction.Item2; if (type == SeatFinder.Nearest) { // For a nearest search, we just return the seat in that // direction. if (posX >= 0 && posX < seats.Count && posY >= 0 && posY < seats[0].Count) { yield return(seats[posX][posY]); } } else { // For a visible search, we start scanning the seats // away from the current position, in the specified // direction. while (posX >= 0 && posX < seats.Count && posY >= 0 && posY < seats[0].Count) { var seat = seats[posX][posY]; if (seat == 'L' || seat == '#') { yield return(seat); break; } posX += direction.Item1; posY += direction.Item2; } } } }
public void ShouldFindSeat(string input, int expectedRow, int expectedCol, int expectedSeatId) { // Arrange var seatFinder = new SeatFinder(); // Act var seat = seatFinder.FindSeat(input); // Assert seat.Row.Should().Be(expectedRow); seat.Col.Should().Be(expectedCol); seat.Id.Should().Be(expectedSeatId); }
public int Handle(char[] input) { bool isFirstHalf = input[0] == _firstHalfIdentifier; if (_min + 1 == _max) { return(isFirstHalf ? _min : _max); } var midPoint = _min + (_max - _min + 1) / 2; var seatFinder = new SeatFinder(_firstHalfIdentifier, isFirstHalf ? _min : midPoint, isFirstHalf ? midPoint - 1 : _max); return(seatFinder.Handle(input.Skip(1).ToArray())); }
/// <summary> /// Update the seating chart based on the rules provided. /// </summary> /// <param name="type">The type of search - e.g. nearest or visible.</param> /// <param name="threshold">The threshold for when to leave a seat unoccupied.</param> /// <returns>The number of seats that were changed.</returns> public int Update(SeatFinder type, int threshold) { int updated = 0; // We make a copy of the list to modify. var newList = seats.Select(x => x.ToList()).ToList(); for (int i = 0; i < seats.Count; ++i) { for (int j = 0; j < seats[0].Count; ++j) { // Look through the nearest seats, and count the number of occupied neighbours. var seat = seats[i][j]; var nearestNeighbours = GetNearestSeats(type, i, j); var occupiedNeigbours = nearestNeighbours.Count(x => x == '#'); if (seat == 'L') { // If the seat is empry, and all neighbours are unoccupied, we // fill the seat. if (occupiedNeigbours == 0) { newList[i][j] = '#'; ++updated; } } else if (seat == '#') { // If the number of occupied seats is larger than the threshold, // we empty the seat. if (occupiedNeigbours >= threshold) { newList[i][j] = 'L'; ++updated; } } } } seats = newList; return(updated); }
void Start() { currentState = CustomerState.WALKING; exitLocation = GameObject.FindWithTag("CustomerSpawn").transform; customerFunctions = new Dictionary <CustomerState, Action <GameObject> >(); customerFunctions.Add(CustomerState.WAITING_FOR_ORDER, getFoodFromPlayer); customerFunctions.Add(CustomerState.WAITING_TO_ORDER, showOrder); customerFunctions.Add(CustomerState.WAITING_TO_PAY, PayForFood); seatMgr = UnityEngine.Object.FindObjectOfType <SeatFinder>(); charaController = GetComponentInChildren <AICharacterControl>(); int seatIndex = seatMgr.getUnoccupiedSeatIndex(); GameObject seat = seatMgr.occupySeat(seatIndex); MoveToSeat(seat); StartCoroutine(StartOrdering()); }
public string GetSolution() { var inputData = _inputService.GetInput(resourceLocation); var result = 0; foreach (var input in inputData) { var rowSeatFinder = new SeatFinder('F', 0, 127); var rowSection = input.Substring(0, 7); var row = rowSeatFinder.Handle(rowSection.ToCharArray()); var seatFinder = new SeatFinder('L', 0, 7); var columnSection = input.Substring(7, 3); var column = seatFinder.Handle(columnSection.ToCharArray()); var seatId = row * 8 + column; if (seatId > result) { result = seatId; } } return($"{result}"); }
public void Example1() { var input = @"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 seats = SeatParser.Parse2(input); var seatFinder = new SeatFinder(); Map currentMap = null; var cont = false; do { var map = currentMap?.DeepCopy(); currentMap = seatFinder.Execute(currentMap ?? seats); _testOutputHelper.WriteLine(currentMap?.Print() ?? "Map was null"); if (map == null) { cont = true; } else if (map.Compare(currentMap)) { cont = false; } } while (cont); var spaces = currentMap !.GetAllSpaces().Count(x => x is OccupiedSeat); Assert.Equal(37, spaces); }
public void FindTwoSeatsOnFrontRow() { Theater theater = Theater.StandardTheater(); List <Seat> booked = new List <Seat> { new Seat("B2"), new Seat("B3"), new Seat("B4"), new Seat("B5") }; SeatFinder seatFinder = new SeatFinder(theater, booked); var expected = new List <Seat> { new Seat("A5"), new Seat("A6") }; var actual = seatFinder.Suggest(2); CollectionAssert.AreEqual(expected, actual); }
public string GetSolution() { var inputData = _inputService.GetInput(resourceLocation); var takenSeats = new List <Seat>(); foreach (var input in inputData) { var rowSeatFinder = new SeatFinder('F', 0, 127); var rowSection = input.Substring(0, 7); var row = rowSeatFinder.Handle(rowSection.ToCharArray()); var seatFinder = new SeatFinder('L', 0, 7); var columnSection = input.Substring(7, 3); var column = seatFinder.Handle(columnSection.ToCharArray()); var seatId = row * 8 + column; takenSeats.Add(new Seat(seatId, row, column)); } var sortedSeatsWithoutFirstAndLastRow = takenSeats.Where(y => y.Row > 0 && y.Row < 127).OrderBy(y => y.SeatId).ThenBy(y => y.Column).ToArray(); var nextSeat = sortedSeatsWithoutFirstAndLastRow.Where((s, i) => i != 0 && sortedSeatsWithoutFirstAndLastRow[i - 1].SeatId == s.SeatId - 2).Single(); return($"{nextSeat.SeatId - 1}"); }
static void Main(string[] args) { var seats = new List <Seat>(); foreach (var item in FlightData.Seats) { var seat = new Seat { code = new Queue <char>(), UpperRow = 127, LowerRow = 0, UpperColumn = 7, LowerColumn = 0 }; foreach (var character in item) { seat.code.Enqueue(character); } SeatFinder.findSeat(seat); seats.Add(seat); } var seatNumbers = seats.Select(x => x.seatNumber).ToList(); var ascending = seatNumbers.OrderBy(i => i).ToList(); //ascending.ForEach(x => Console.WriteLine(x)); var lowest = ascending[0]; var highest = ascending[ascending.Count - 1]; var fullList = Enumerable.Range(lowest, highest - lowest).ToList(); IEnumerable <int> differenceQuery = fullList.Except(ascending); foreach (int i in differenceQuery) { Console.WriteLine(i); } }
void Start() { seatMgr = UnityEngine.Object.FindObjectOfType <SeatFinder>(); }