public static List <Seat> GetAdjacentSeatsWithinDistance(List <Seat> seats, Seat seat) { return(seats.Where(s => IsWithinDistance(s, seat)) .Select(s => s) .ToList()); }
private static void Main() { string InputFileName = @"..\..\..\Input.txt"; string[] inputLines = File.ReadAllLines(InputFileName); maxX = inputLines[0].Length + 2; maxY = inputLines.Length + 2; Seat[,] map = new Seat[maxX, maxY]; Seat[,] mapNext = new Seat[maxX, maxY]; for (int y = 0; y < maxY; y++) { for (int x = 0; x < maxX; x++) { if (IsBorder(x, y)) { map[x, y] = Seat.Floor; } else { map[x, y] = CharToSeat(inputLines[y - 1][x - 1]); } } } int changes; int step = 0; do { changes = 0; for (int y = 0; y < maxY; y++) { for (int x = 0; x < maxX; x++) { if (IsBorder(x, y)) { mapNext[x, y] = Seat.Floor; } else { mapNext[x, y] = NextStepPart1(map, x, y); if (map[x, y] != mapNext[x, y]) { changes++; } } } } Seat[,] t = map; map = mapNext; mapNext = t; step++; } while (changes != 0); int occSeats = 0; for (int i = 0; i < maxY; i++) { for (int j = 0; j < maxX; j++) { if (map[j, i] == Seat.Occupied) { occSeats++; } } } Console.WriteLine($"Part1: occupied seats: {occSeats} (right answer: 2344)"); // Part 2 step = 0; occSeats = 0; for (int y = 0; y < maxY; y++) { for (int x = 0; x < maxX; x++) { if (IsBorder(x, y)) { map[x, y] = Seat.Floor; } else { map[x, y] = CharToSeat(inputLines[y - 1][x - 1]); } } } do { changes = 0; for (int y = 0; y < maxY; y++) { for (int x = 0; x < maxX; x++) { if (IsBorder(x, y)) { mapNext[x, y] = Seat.Floor; } else { mapNext[x, y] = NextStepPart2(map, x, y); if (map[x, y] != mapNext[x, y]) { changes++; } } } } Seat[,] t = map; map = mapNext; mapNext = t; step++; } while (changes != 0); for (int i = 0; i < maxY; i++) { for (int j = 0; j < maxX; j++) { if (map[j, i] == Seat.Occupied) { occSeats++; } } } Console.WriteLine($"Part2: occupied seats: {occSeats} (right answer: 2076)"); Console.ReadKey(); }
public static List <Seat> GetAdjacentSeats(List <Seat> seats, Seat seat) { return(seats.Where(s => IsImmediateAdjacentSeat(s, seat)) .Select(s => s) .ToList()); }