1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Advent Of Code 2021 (Day 11: Dumbo Octopus)をElixirで楽しむ

Last updated at Posted at 2022-05-06

いにしへの奈良の都の八重桜今日九重ににほひぬるかな

Advent Calendar 2022 113日目1の記事です。
I'm looking forward to 12/25,2022 :santa::santa_tone1::santa_tone2::santa_tone3::santa_tone4::santa_tone5:
私のAdvent Calendar 2022 一覧


はじめに

この記事は、Advent Of Code 2021 Day 11: Day 11: Dumbo OctopusElixirで楽しんでみます。

私はGitHubでログインしました。

私の回答

私の回答です。

私の回答
input = """
5483143223
2745854711
5264556173
6141336146
6357385478
4167524645
2176841721
6882881134
4846848554
5283751526
"""
grid =
  for {line, row} <- Enum.with_index(String.split(input, "\n", trim: true)),
      {number, col} <- Enum.with_index(String.to_charlist(line)),
      into: %{} do
    {{row, col}, number - ?0}
  end

defmodule DumboOctopus do
  def step(grid) do
    {grid, flashes} =
      grid
      |> Enum.reduce({%{}, []}, fn
        {point, 9}, {acc_grid, acc_flashes} ->
          {Map.merge(acc_grid, %{point => 10}), [point | acc_flashes]}

        {point, number}, {acc_grid, acc_frashes} ->
          {Map.merge(acc_grid, %{point => number + 1}), acc_frashes}
      end)

    step(grid, flashes)
  end

  defp step(grid, []) do
    grid
    |> Enum.reduce({%{}, 0}, fn {point, number}, {acc_map, acc_cnt} ->
      case number >= 10 do
        true ->
          {Map.merge(acc_map, %{point => 0}), acc_cnt + 1}

        false ->
          {Map.merge(acc_map, %{point => number}), acc_cnt}
      end
    end)
  end

  defp step(grid, flashes) do
    {grid, flashes} =
      flashes
      |> Enum.reduce({grid, []}, fn {row, col}, {grid, flashes} ->
        [
          {row - 1, col},
          {row + 1, col},
          {row, col - 1},
          {row, col + 1},
          {row - 1, col - 1},
          {row - 1, col + 1},
          {row + 1, col - 1},
          {row + 1, col + 1}
        ]
        |> Enum.reduce({grid, flashes}, fn point, {acc_grid, acc_frashes} ->
          case acc_grid[point] do
            nil -> {acc_grid, acc_frashes}
            9 -> {Map.merge(acc_grid, %{point => 10}), [point | acc_frashes]}
            number -> {Map.merge(acc_grid, %{point => number + 1}), acc_frashes}
          end
        end)
      end)

    step(grid, flashes)
  end
end

print = fn grid ->
  rows = String.split(input, "\n", trim: true) |> Enum.count()
  cols = div(Enum.count(grid), rows)

  for(i <- 0..(rows - 1), j <- 0..(cols - 1), do: grid[{i, j}])
  |> Enum.chunk_every(cols)
  |> IO.inspect()
end

Part 1

Enum.reduce(1..100, {grid, 0}, fn _, {acc_grid, acc_cnt} ->
  {new_grid, cnt} = DumboOctopus.step(acc_grid)
  #print.(new_grid)
  {new_grid, acc_cnt + cnt}
end)
|> IO.inspect()

Part 2

Enum.reduce_while(Stream.iterate(1, &(&1 + 1)), {grid, 0}, fn step, {acc_grid, _} ->
  {new_grid, _cnt} = DumboOctopus.step(acc_grid)

  all_zero? =
    new_grid
    |> Map.values()
    |> Enum.all?(&(&1 == 0))

  if all_zero?, do: {:halt, {new_grid, step}}, else: {:cont, {new_grid, step}}
end)
|> IO.inspect()

It works!
Amazing!

お手本

Day 11のお手本(José Valimさんの動画)はないようです :sunglasses:

もし存在をご存知の方はお知らせいただけますとありがたいです! :pray::pray_tone1::pray_tone2::pray_tone3::pray_tone4::pray_tone5:


Wrapping up :lgtm::lgtm::lgtm::lgtm::lgtm:

Advent Of Code 2021 Day 11: Dumbo OctopusElixirで楽しんでみました。
Day 25まであるので引き続き楽しんでいきたいとおもいます。

It works!
Amazing!

自分で解いてみて、なんだかイマイチだなあとおもいながら、動画をみることでJosé Valimさんに特別家庭教師をしてもらっている気に勝手になっています :sweat_smile:
海綿が水を吸うように、Elixirのイケている書き方を吸収しています。
伸びしろしかありません。

Enjoy Elixir:bangbang::bangbang::bangbang:
$\huge{Enjoy\ Elixir🚀}$

以上です。


I organize autoracex.
And I take part in NervesJP, fukuoka.ex, EDI, tokyo.ex, Pelemay.
I hope someday you'll join us.

We Are The Alchemists, my friends!

  1. @kaizen_nagoya さんの「「@e99h2121 アドベントカレンダーではありますまいか Advent Calendar 2020」の改訂版ではありますまいか Advent Calendar 2022 1日目 Most Breakthrough Generator」から着想を得て、模倣いたしました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?