Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Day 11 - Advent of Code 2020

lib/advent_of_code/2020/day-11.livemd

Day 11 - Advent of Code 2020

# Mix.install([:kino, :benchee])
alias AdventOfCode.CharGrid, as: Grid

Links

Prompt

— 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?

To begin, get your puzzle input.

— 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?

Although it hasn’t changed, you can still get your puzzle input.

Input

input = Kino.Input.textarea("Please paste your input file:")
input = input |> Kino.Input.read()
input = AdventOfCode.Input.get!(11, 2020)
"LL.LL.LLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLL.LLLL.LLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLLLL\nLLLLLLLLLLLLLLLLLLL.LLLLL.LLLLLLL.LLLLLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLL.LLLL.LLLL.LLLLLLLL\n.LLLL.LLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLL.LLLL.LLLLLLLL.LLLLLLLL.LL.LLLL.LLLLLLLLL.LLLLLLLL\nLLLLL.LLLLLL.LLLLLL.LLLLLLLLLLLLL.LLLLLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL\nLLLLLLLLLLLL.LLLLLL.LLLLL.LLLLLLL.LLLLLLL.LLLL.LLLLLLLL.LLLLLLLLLLLLLLLL.LLL.LLLLL.LLLLLLLL\n.LLLL.LLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLL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LL.LLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLL\nL.LLL.LLLLLL.LLLLLL.LLLLL.LLL.LLL.LLLLLLL.LLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LL.LLLLLLLL\nL.LLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLL.LLLL.LLLLLLLL.LL.LLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLL\nLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLL.LLLL.LLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLL.\nLLLLLLLLLLLL.LLLLLL.LLLLL.LLLLLLL.LLLLLLLLLLLLLL" <> ...

Solution

defmodule Day11 do
  alias AdventOfCode.CharGrid, as: Grid

  defdelegate parse(input), to: __MODULE__.Input

  def part1(input) do
    parse(input) |> process_p1()
  end

  def part2(input) do
    parse(input) |> process_p2()
  end

  defp process_p1(%Grid{} = grid) do
    new_grid =
      Enum.map(grid.grid, fn
        {xy, ?L} ->
          grid
          |> Grid.adjacent_values(xy)
          |> Enum.any?(&amp;(&amp;1 == ?#))
          |> then(fn
            true -> {xy, ?L}
            false -> {xy, ?#}
          end)

        {xy, ?#} ->
          grid
          |> Grid.adjacent_values(xy)
          |> Enum.count(&amp;(&amp;1 == ?#))
          |> then(fn
            x when x >= 4 -> {xy, ?L}
            _ -> {xy, ?#}
          end)

        {xy, ?.} ->
          {xy, ?.}
      end)
      |> Enum.into(%{})

    if new_grid == grid.grid,
      do: Grid.count_chars(grid, ?#),
      else: process_p1(%{grid | grid: new_grid})
  end

  defp process_p2(%Grid{} = grid) do
    new_grid =
      Enum.map(grid.grid, fn
        {xy, ?L} ->
          grid
          |> Grid.lines_of_values(xy)
          |> Enum.map(fn line -> Enum.find(line, &amp;(&amp;1 != ?.)) end)
          |> Enum.any?(&amp;(&amp;1 == ?#))
          |> then(fn
            true -> {xy, ?L}
            false -> {xy, ?#}
          end)

        {xy, ?#} ->
          grid
          |> Grid.lines_of_values(xy)
          |> Enum.map(fn line -> Enum.find(line, &amp;(&amp;1 != ?.)) end)
          |> Enum.count(&amp;(&amp;1 == ?#))
          |> then(fn
            x when x >= 5 -> {xy, ?L}
            _ -> {xy, ?#}
          end)

        {xy, ?.} ->
          {xy, ?.}
      end)
      |> Enum.into(%{})

    if new_grid == grid.grid,
      do: Grid.count_chars(grid, ?#),
      else: process_p2(%{grid | grid: new_grid})
  end

  defmodule Input do
    def parse(input) when is_binary(input) do
      Grid.from_input(input)
    end
  end
end
{:module, Day11, <<70, 79, 82, 49, 0, 0, 21, ...>>,
 {:module, Day11.Input, <<70, 79, 82, ...>>, {:parse, 1}}}

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

Your puzzle answer was 2281.

Day11.part1(input)
2281

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

Your puzzle answer was 2085.

Day11.part2(input)
2085

Both parts of this puzzle are complete! They provide two gold stars: **

At this point, you should return to your Advent calendar and try another puzzle.

If you still want to see it, you can get your puzzle input.

Tests

ExUnit.start(auto_run: false)

defmodule Day11Test do
  use ExUnit.Case, async: false

  setup_all do
    [
      input:
        "L.LL.LL.LL\nLLLLLLL.LL\nL.L.L..L..\nLLLL.LL.LL\nL.LL.LL.LL\nL.LLLLL.LL\n..L.L.....\nLLLLLLLLLL\nL.LLLLLL.L\nL.LLLLL.LL"
    ]
  end

  describe "part1/1" do
    test "returns expected value", %{input: input} do
      assert Day11.part1(input) == 37
    end
  end

  describe "part2/1" do
    test "returns expected value", %{input: input} do
      assert Day11.part2(input) == 26
    end
  end
end

ExUnit.run()
..
Finished in 0.00 seconds (0.00s async, 0.00s sync)
2 tests, 0 failures

Randomized with seed 779649
%{total: 2, failures: 0, excluded: 0, skipped: 0}