5 votes

Day 25: Sea Cucumber

Today's problem description: https://adventofcode.com/2021/day/25

Please post your solutions in your own top-level comment. Here's a template you can copy-paste into your comment to format it nicely, with the code collapsed by default inside an expandable section with syntax highlighting (you can replace python with any of the "short names" listed in this page of supported languages):

<details>
<summary>Part 1</summary>

```python
Your code here.
```

</details>

1 comment

  1. jzimbel
    Link
    Elixir Pretty quick and simple. Posting my messy code as-is and going to bed. Merry Christmas, folks. Part 1 defmodule AdventOfCode.Solution.Year2021.Day25 do def part1(input) do {grid, width,...

    Elixir

    Pretty quick and simple. Posting my messy code as-is and going to bed. Merry Christmas, folks.

    Part 1
    defmodule AdventOfCode.Solution.Year2021.Day25 do
      def part1(input) do
        {grid, width, height} = parse_input(input)
    
        grid
        |> Stream.iterate(&step(&1, width, height))
        |> Stream.chunk_every(2, 1, :discard)
        |> Stream.with_index(1)
        |> Enum.find(&match?({[grid, grid], _}, &1))
        |> elem(1)
      end
    
      def part2(_input) do
      end
    
      defp step(grid, width, height) do
        grid
        |> Enum.into(%{}, fn
          {{x, y}, ?>} = cuke ->
            move_to = {rem(x + 1, width), y}
    
            case grid[move_to] do
              nil -> {move_to, ?>}
              _ -> cuke
            end
    
          down ->
            down
        end)
        |> then(fn grid ->
          Enum.into(grid, %{}, fn
            {{x, y}, ?v} = cuke ->
              move_to = {x, rem(y + 1, height)}
    
              case grid[move_to] do
                nil -> {move_to, ?v}
                _ -> cuke
              end
    
            right ->
              right
          end)
        end)
      end
    
      defp parse_input(input) do
        charlists =
          input
          |> String.split()
          |> Enum.map(&String.to_charlist/1)
    
        height = length(charlists)
        width = length(hd(charlists))
    
        grid =
          for {line, y} <- Enum.with_index(charlists),
              {char, x} <- Enum.with_index(line),
              char != ?.,
              into: %{},
              do: {{x, y}, char}
    
        {grid, width, height}
      end
    end
    
    1 vote