--- Day 11: Seating System ---

Your plane lands with plenty of time to spare. The final leg of your journey is a ferry that goes directly to the tropical island where you can finally start your vacation. As you reach the waiting area to board the ferry, you realize you're so early, nobody else has even arrived yet!

By modeling the process people use to choose (or abandon) their seat in the waiting area, you're pretty sure you can predict the best place to sit. You make a quick map of the seat layout (your puzzle input).

The seat layout fits neatly on a grid. Each position is either floor (.), an empty seat (L), or an occupied seat (#). For example, the initial seat layout might look like this:

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

Now, you just need to model the people who will be arriving shortly. Fortunately, people are entirely predictable and always follow a simple set of rules. All decisions are based on the number of occupied seats adjacent to a given seat (one of the eight positions immediately up, down, left, right, or diagonal from the seat). The following rules are applied to every seat simultaneously:

  • If a seat is empty (L) and there are no occupied seats adjacent to it, the seat becomes occupied.
  • If a seat is occupied (#) and four or more seats adjacent to it are also occupied, the seat becomes empty.
  • Otherwise, the seat's state does not change.

Floor (.) never changes; seats don't move, and nobody sits on the floor.

After one round of these rules, every seat in the example layout becomes occupied:

#.##.##.##
#######.##
#.#.#..#..
####.##.##
#.##.##.##
#.#####.##
..#.#.....
##########
#.######.#
#.#####.##

After a second round, the seats with four or more occupied adjacent seats become empty again:

#.LL.L#.##
#LLLLLL.L#
L.L.L..L..
#LLL.LL.L#
#.LL.LL.LL
#.LLLL#.##
..L.L.....
#LLLLLLLL#
#.LLLLLL.L
#.#LLLL.##

This process continues for three more rounds:

#.##.L#.##
#L###LL.L#
L.#.#..#..
#L##.##.L#
#.##.LL.LL
#.###L#.##
..#.#.....
#L######L#
#.LL###L.L
#.#L###.##
#.#L.L#.##
#LLL#LL.L#
L.L.L..#..
#LLL.##.L#
#.LL.LL.LL
#.LL#L#.##
..L.L.....
#L#LLLL#L#
#.LLLLLL.L
#.#L#L#.##
#.#L.L#.##
#LLL#LL.L#
L.#.L..#..
#L##.##.L#
#.#L.LL.LL
#.#L#L#.##
..L.L.....
#L#L##L#L#
#.LLLLLL.L
#.#L#L#.##

At this point, something interesting happens: the chaos stabilizes and further applications of these rules cause no seats to change state! Once people stop moving around, you count 37 occupied seats.

Simulate your seating area by applying the seating rules repeatedly until no seats change state. How many seats end up occupied?

Rust Solution

use rustgym_util::*;
use std::fmt::Write;
use std::io::*;

pub fn solve(reader: &mut dyn BufRead, writer: &mut dyn Write) {
    let it = reader.lines().map(|l| l.unwrap());
    let grid: Vec<Vec<char>> = it.map(|s| s.chars().collect()).collect();
    let res1 = simulate1(grid.clone());
    let res2 = simulate2(grid);
    writeln!(writer, "{}", res1).unwrap();
    writeln!(writer, "{}", res2).unwrap();
}

fn simulate1(mut grid: Vec<Vec<char>>) -> i32 {
    let n = grid.len();
    let m = grid[0].len();
    loop {
        let mut occupied = 0;
        let mut change = 0;
        let mut next_grid = vec![vec!['.'; m]; n];
        for i in 0..n {
            for j in 0..m {
                let mut count = 0;
                for &di in &[-1, 0, 1] {
                    for &dj in &[-1, 0, 1] {
                        if di == 0 && dj == 0 {
                            continue;
                        }
                        let ii = i as i32 + di;
                        let jj = j as i32 + dj;
                        if ii < 0 || ii >= n as i32 || jj < 0 || jj >= m as i32 {
                            continue;
                        }
                        if grid[ii as usize][jj as usize] == '#' {
                            count += 1;
                        }
                    }
                }
                next_grid[i][j] = grid[i][j];
                if count == 0 && grid[i][j] == 'L' {
                    next_grid[i][j] = '#';
                    change += 1;
                }
                if count >= 4 && grid[i][j] == '#' {
                    next_grid[i][j] = 'L';
                    change += 1;
                }
                if next_grid[i][j] == '#' {
                    occupied += 1;
                }
            }
        }
        if change == 0 {
            break occupied;
        }
        grid = next_grid;
    }
}

fn simulate2(mut grid: Vec<Vec<char>>) -> i32 {
    let n = grid.len();
    let m = grid[0].len();
    loop {
        let mut occupied = 0;
        let mut change = 0;
        let mut next_grid = vec![vec!['.'; m]; n];
        for i in 0..n {
            for j in 0..m {
                let mut count = 0;
                for &di in &[-1, 0, 1] {
                    for &dj in &[-1, 0, 1] {
                        if di == 0 && dj == 0 {
                            continue;
                        }
                        let mut step = 1;
                        loop {
                            let ii = i as i32 + di * step;
                            let jj = j as i32 + dj * step;
                            if ii < 0 || ii >= n as i32 || jj < 0 || jj >= m as i32 {
                                break;
                            }

                            if grid[ii as usize][jj as usize] == '#' {
                                count += 1;
                                break;
                            }
                            if grid[ii as usize][jj as usize] == 'L' {
                                break;
                            }
                            step += 1;
                        }
                    }
                }
                next_grid[i][j] = grid[i][j];
                if count == 0 && grid[i][j] == 'L' {
                    next_grid[i][j] = '#';
                    change += 1;
                }
                if count >= 5 && grid[i][j] == '#' {
                    next_grid[i][j] = 'L';
                    change += 1;
                }
                if next_grid[i][j] == '#' {
                    occupied += 1;
                }
            }
        }
        if change == 0 {
            break occupied;
        }
        grid = next_grid;
    }
}

advent_of_code!(test, "input.txt", "output.txt");

Having problems with this solution? Click here to submit an issue on github.