static void Main(string[] args) { var seatSystem = new SeatSystem(File.ReadAllLines("../input.txt")); //Part One Console.WriteLine(seatSystem.PredictPeopleSeating(4)); //Part Two Console.WriteLine(seatSystem.PredictPeopleSeating(5, true)); }
private static SeatSystem[,] ParseInput(string input) { string[] lines = input.Lines(); int m = lines.Length; int n = lines[0].Length; SeatSystem[,] seats = new SeatSystem[m, n]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { seats[i, j] = (SeatSystem)lines[i][j]; } } return(seats); }
private int CountType(SeatSystem[,] seats, SeatSystem type) { int m = seats.GetLength(0); int n = seats.GetLength(1); int count = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (seats[i, j] == type) { ++count; } } } return(count); }
private (SeatSystem[, ] NewState, bool Changed) TransformSeats(SeatSystem[,] seats, SeatRule emptySeatRule, SeatRule occupiedSeatRule) { int m = seats.GetLength(0); int n = seats.GetLength(1); bool changed = false; SeatSystem[,] nextState = new SeatSystem[m, n]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { nextState[i, j] = seats[i, j]; SeatSystem?update; switch (seats[i, j]) { case SeatSystem.Empty: update = emptySeatRule(seats, i, j); if (update != null) { nextState[i, j] = (SeatSystem)update; changed = true; } break; case SeatSystem.Occupied: update = occupiedSeatRule(seats, i, j); if (update != null) { nextState[i, j] = (SeatSystem)update; changed = true; } break; default: break; } } } return(nextState, changed); }
private int CountAdjacentOfType(SeatSystem[,] seats, int i, int j, SeatSystem type, int target = int.MaxValue) { int rowStart = Math.Max(0, i - 1); int rowStop = Math.Min(seats.GetLength(0), i + 2); int colStart = Math.Max(0, j - 1); int colStop = Math.Min(seats.GetLength(1), j + 2); int count = 0; for (int a = rowStart; a < rowStop; ++a) { for (int b = colStart; b < colStop; ++b) { if (a != i || b != j) { // Return early if we met out target count already if (seats[a, b] == type && ++count >= target) { return(count); } } } } return(count); }
public void PartOneTest() { var subject = new SeatSystem(GetExampleInput()); Assert.Equal(37, subject.PredictPeopleSeating(4)); }
public void PartTwoTest() { var subject = new SeatSystem(GetExampleInput()); Assert.Equal(26, subject.PredictPeopleSeating(5, true)); }