2020-12-11

Day 11: Seating System

Description:
--- 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?

--- Part Two ---

As soon as people start to arrive, you realize your mistake. People don't just care about adjacent seats - they care about the first seat they can see in each of those eight directions!

Now, instead of considering just the eight immediately adjacent seats, consider the first seat in each of those eight directions. For example, the empty seat below would see eight occupied seats:

.......#.
...#.....
.#.......
.........
..#L....#
....#....
.........
#........
...#.....

The leftmost empty seat below would only see one empty seat, but cannot see any of the occupied ones:

.............
.L.L.#.#.#.#.
.............

The empty seat below would see no occupied seats:

.##.##.
#.#.#.#
##...##
...L...
##...##
#.#.#.#
.##.##.

Also, people seem to be more tolerant than you expected: it now takes five or more visible occupied seats for an occupied seat to become empty (rather than four or more from the previous rules). The other rules still apply: empty seats that see no occupied seats become occupied, seats matching no rule don't change, and floor never changes.

Given the same starting layout as above, these new rules cause the seating area to shift around as follows:

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

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

#.LL.LL.L#
#LLLLLL.LL
L.L.L..L..
LLLL.LL.LL
L.LL.LL.LL
L.LLLLL.LL
..L.L.....
LLLLLLLLL#
#.LLLLLL.L
#.LLLLL.L#

#.L#.##.L#
#L#####.LL
L.#.#..#..
##L#.##.##
#.##.#L.##
#.#####.#L
..#.#.....
LLL####LL#
#.L#####.L
#.L####.L#

#.L#.L#.L#
#LLLLLL.LL
L.L.L..#..
##LL.LL.L#
L.LL.LL.L#
#.LLLLL.LL
..L.L.....
LLLLLLLLL#
#.LLLLL#.L
#.L#LL#.L#

#.L#.L#.L#
#LLLLLL.LL
L.L.L..#..
##L#.#L.L#
L.L#.#L.L#
#.L####.LL
..#.#.....
LLL###LLL#
#.LLLLL#.L
#.L#LL#.L#

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

Again, at this point, people stop shifting around and the seating area reaches equilibrium. Once this occurs, you count 26 occupied seats.

Given the new visibility method and the rule change for occupied seats becoming empty, once equilibrium is reached, how many seats end up occupied?

Input:
LLLLLLL.LLLLLLLLLLLLLLL.LLL.LLLL.LLLLLLL.L.LLLLLLL.LLLLLL.LLLLLLLL.LLLLL..LLLLLLLLLLLLLLLLLL
LLLLLLL.LLLLLL.LLLL.LLL.LLLLLLLLLL.LLLLLLL.LLLLL.L.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL
.LLLLLLLLLLLLLLL.LLLLLL.LLLLLL.LLLLLLLL.LL.LLLLLLL.LLL.LL.LLLLLLLLLLL.LLL..LLLLLLLL.LLLLLLLL
LLLLLLLLLLLLLLL..LLLLLL.LLLLLLLLLLLLL.LLLL.LLLLLLLLLLLL.L.LLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL
LLLLL.L.LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLLLLL.LLL..LLL.LLLLLL.LLLLLL.LLLL.LLLL.L.LLLLLL
LLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLL.L.LL.LLLL.LLLLLL..LLLLLL.LLLLLLLL..LLLLL.LLLLLLLL..LLLLLLLL
LLLLLLL.LLLLLLLL.LLLLLL.LLLLL.L.LLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLL
.LLLLLL..LLLLLLL.LLLLLL.LLLLL.LLLLLLLLLLLL.L.LLLLLLLLLLLLLLLLLLLLL.LLLLLL.LLL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LLLLLLL.LLLLLLLL
LLLLLLL.LLLL.LLLLLLLLLLL.LLLL.LLLLLLLLLL.L.LLLLLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLL
LLL......LL...L...L..LL..L..L......L...L.L..L.........L.LL.LL..L..L.L......L..L........L..L.
LL.LLLLLLLLLLLLL.LL.LLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLL.LLLLLLLLLLL
LLLLLLLLLLLLLL.L.LLLLLLLLLLLL.LL.LLLL..LLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLL.LL.LLLLLLLL
LLLLLLL.LLLLLLLL.LLLLLL.LLLLL.LLLLLLL.LLLLLLLLLL.L.LLLLLL.LLLLLLLL..LLLLL.LLLLLLLLL.LLLLLLLL
LLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLL.LLLLLLLLLLL.LLL.LLLLLL.LL.LLLLLL.LLLLLLLL
LLLLLLLLLLLLLLLL.LLLLLL.LLL.L.LLLLLLL.LLLLLLLLLLLL.LLLLLLLLL.LLL.L.LLLLLL.LLLLLLLLLLLLLLLLLL
LLLLLLL.LLLLL.LL.LL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL..LLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLL
LLL.LLLLLLLLLLLL.LLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLL.LLLLLL.LLL.LLLLLLLLLLL.LLLLLLLLL.LLLLLLLL

``````use crate::common::AdventOfCodeDay;

use std::fmt::Display;

#[derive(Debug, PartialEq, Clone)]
enum BoardCell {
Floor,
Empty,
Full,
}

#[derive(Clone)]
struct Board {
cells: Vec<Vec<BoardCell>>,
width: usize,
height: usize,
}

impl Board {
fn parse(dat: &str) -> Board {
Board {
height: dat.lines().count(),
width:  dat.lines().next().unwrap().len(),
cells:  dat.lines().map(Board::parse_line).collect()
}
}

fn parse_line(dat: &str) -> Vec<BoardCell> {
dat.chars().map(|c| match c {
'.' => BoardCell::Floor,
'L' => BoardCell::Empty,
'#' => BoardCell::Full,
_   => panic!(),
}).collect()
}

fn adjac(&self, x: i32, y: i32) -> u32 {
let mut c = 0;

for dx in -1..=1 {
for dy in -1..=1 {
if dx == 0 && dy == 0 {
continue;
}
if x+dx < 0 || y+dy < 0 || x+dx >= self.width as i32 || y+dy >= self.height as i32 {
continue;
}
if self.cells[(y+dy) as usize][(x+dx) as usize] == BoardCell::Full {
c+=1;
}
}
}

return c;
}

fn adjac2(&self, x: i32, y: i32) -> u32 {
let mut c = 0;

for dx in -1..=1 {
for dy in -1..=1 {
if dx == 0 && dy == 0 {
continue;
}

if x+dx < 0 || y+dy < 0 || x+dx >= self.width as i32 || y+dy >= self.height as i32 {
continue;
}
if self.adjac_ray(x, y, dx, dy) {
c+=1;
}
}
}

return c;
}

fn adjac_ray(&self, x : i32, y: i32, dx: i32, dy: i32) -> bool {
for i in 1.. {
let rx = x + dx * i;
let ry = y + dy * i;

if rx < 0 || ry < 0 || rx >= self.width as i32 || ry >= self.height as i32 {
return false;
}

if self.cells[ry as usize][rx as usize] == BoardCell::Full {
return true;
}
if self.cells[ry as usize][rx as usize] == BoardCell::Empty {
return false;
}
}

panic!();
}

fn step(&self) -> Board {

let mut nc = self.cells.clone();

for y in 0..self.height {
for x in 0..self.width {
if self.cells[y][x] == BoardCell::Full {
if self.adjac(x as i32, y as i32) >= 4 {
nc[y][x] = BoardCell::Empty; // If a seat is occupied (#) and four or more seats adjacent to it are also occupied, the seat becomes empty.
}
} else if self.cells[y][x] == BoardCell::Empty {
if self.adjac(x as i32, y as i32) == 0 {
nc[y][x] = BoardCell::Full; // If a seat is empty (L) and there are no occupied seats adjacent to it, the seat becomes occupied.
}
}
}
}

Board {
width: self.width,
height: self.height,

cells: nc,
}
}

fn step2(&self) -> Board {

let mut nc = self.cells.clone();

for y in 0..self.height {
for x in 0..self.width {
if self.cells[y][x] == BoardCell::Full {
if self.adjac2(x as i32, y as i32) >= 5 {
nc[y][x] = BoardCell::Empty; // it now takes five or more visible occupied seats for an occupied seat to become empty
}
} else if self.cells[y][x] == BoardCell::Empty {
if self.adjac2(x as i32, y as i32) == 0 {
nc[y][x] = BoardCell::Full; // If a seat is empty (L) and there are no occupied seats adjacent to it, the seat becomes occupied.
}
}
}
}

Board {
width: self.width,
height: self.height,

cells: nc,
}
}

fn total_occupied(&self) -> u32 {
self.cells.iter().map(|p| p.iter().filter(|p| **p == BoardCell::Full).count() as u32).sum()
}
}

impl PartialEq for Board {
fn eq(&self, other: &Self) -> bool {
if self.width != other.width {
return false;
}
if self.height != other.height {
return false;
}

for y in 0..self.height {
for x in 0..self.width {
if self.cells[y][x] != other.cells[y][x] {
return false;
}
}
}

return true;
}
}

impl Display for Board {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut r = String::with_capacity(self.width * self.height + self.height + 1);
for y in 0..self.height {
for x in 0..self.width {
r.push_str(match self.cells[y][x] {
BoardCell::Floor => ".",
BoardCell::Empty => "L",
BoardCell::Full  => "#",
});
}
r.push_str("\n");
}
writeln!(f, "{}", r)
}
}

pub struct Day11 {
input: Board,
}

impl Day11 {
pub fn new() -> Self {
let input_bytes = include_bytes!("../res/11_input.txt");
let input_str = String::from_utf8_lossy(input_bytes);

Self {
input: Board::parse(&input_str)
}
}
}

verboseln!("{}", self.input);
verboseln!("> Initial");

let mut board = self.input.clone();
loop {
let newboard = board.step();

verboseln!("{}", newboard);
verboseln!("> {}", newboard.total_occupied());
verboseln!();

if newboard == board {
return newboard.total_occupied().to_string();
}

board = newboard;
}
}

verboseln!("{}", self.input);
verboseln!("> Initial");

let mut board = self.input.clone();
loop {
let newboard = board.step2();

verboseln!("{}", newboard);
verboseln!("> {}", newboard.total_occupied());
verboseln!();

if newboard == board {
return newboard.total_occupied().to_string();
}

board = newboard;
}
}
}``````
Result Part 1: 2321
Result Part 2: 2102

made with vanilla PHP and MySQL, no frameworks, no bootstrap, no unnecessary* javascript