0

posted at

updated at

Organization

# Advent Of Code 2021 (Day 4: Giant Squid)をElixirで楽しむ

I'm looking forward to 12/25,2022

# はじめに

この記事は、Advent Of Code 2021 Day 4: Giant SquidElixirで楽しんでみます。

# 私の回答

``````input = """
7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1

22 13 17 11  0
8  2 23  4 24
21  9 14 16  7
6 10  3 18  5
1 12 20 15 19

3 15  0  2 22
9 18 13 17  5
19  8  7 25 23
20 11 10 24  4
14 21 16 12  6

14 21 17 24  4
10 16 15  9 19
18  8 23 26 20
22 11 13  6  5
2  0 12  3  7
"""
``````

## Part 1

``````[randoms | boards] = input
|> String.split("\n\n", trim: true)

list_of_randoms = String.split(randoms, ",") |> Enum.map(&String.to_integer/1)

list_of_boards = for i <- 0..Enum.count(boards) - 1 do
Enum.at(boards, i)
|> String.split("\n", trim: true)
|> Enum.map(fn row -> String.split(row, " ", trim: true) |> Enum.map(&String.to_integer/1) end)
end

defmodule Recursion do
def recur([], list_of_boards), do: list_of_boards

def recur([value | tail], list_of_boards) do
new_list_of_boards_or_answer = Enum.reduce_while(list_of_boards, [], fn board, acc ->
new_board = draw_board(board, value)

if bingo?(new_board) or bingo?(transpose(new_board)) do
else
{:cont, acc ++ [new_board]}
end
end)

else
end
end

defp draw_board(board, value) do
Enum.reduce(board, [], fn row, acc ->
new_row = Enum.map(row, fn
^value -> 0
v -> v
end)

acc ++ [new_row]
end)
end

defp transpose(board) do
for pos <- 0..4 do
Enum.map(board, & Enum.at(&1, pos))
end
end

defp bingo?(board) do
Enum.map(board, &bingo_row?/1) |> Enum.any?(& &1)
end

defp bingo_row?([0, 0, 0, 0, 0]), do: true

defp bingo_row?(_), do: false

board
|> Enum.map(&Enum.sum/1)
|> Enum.sum()
|> Kernel.*(value)
end
end

list_of_randoms
|> Recursion.recur(list_of_boards)
``````

## Part 2

``````[randoms | tables] = input
|> String.split("\n\n", trim: true)

list_of_randoms = String.split(randoms, ",") |> Enum.map(&String.to_integer/1)

list_of_boards = for i <- 0..Enum.count(boards) - 1 do
Enum.at(boards, i)
|> String.split("\n", trim: true)
|> Enum.map(fn row -> String.split(row, " ", trim: true) |> Enum.map(&String.to_integer/1) end)
end

defmodule Recursion do
def recur([], list_of_boards), do: list_of_boards

def recur([value | tail], list_of_boards) do
new_list_of_boards_or_answer = Enum.reduce_while(list_of_boards, [], fn board, acc ->
new_board = draw_board(board, value)

if bingo?(new_board) or bingo?(transpose(new_board)) do
if Enum.count(list_of_boards) > 1 do
{:cont, acc}
else
end
else
{:cont, acc ++ [new_board]}
end
end)

else
end
end

defp draw_board(board, value) do
Enum.reduce(board, [], fn row, acc ->
new_row = Enum.map(row, fn
^value -> 0
v -> v
end)

acc ++ [new_row]
end)
end

defp transpose(board) do
for pos <- 0..4 do
Enum.map(board, & Enum.at(&1, pos))
end
end

defp bingo?(board) do
Enum.map(board, &bingo_row?/1)
|> Enum.any?(& &1)
end

defp bingo_row?([0, 0, 0, 0, 0]), do: true

defp bingo_row?(_), do: false

board
|> Enum.map(&Enum.sum/1)
|> Enum.sum()
|> Kernel.*(value)
end
end

list_of_randoms
|> Recursion.recur(list_of_boards)
``````

It works!
Amazing!

お見逃し無く！

# お手本

José ValimさんがLivebook楽しまれている動画があります。
José Valimさんは、Elixirの作者です！

いきなり正解を書くわけではなく、少しずつ試しながら作っていって、リファクタしていくさまがJosé Valimさんの息遣いでみれるのでとても参考になります。

お手本

まだ見ていないのです

# Wrapping up

Advent Of Code 2021 Day 4: Giant SquidElixirで楽しんでみました。
Day 25まであるので引き続き楽しんでいきたいとおもいます。
だんだん、私には難しくなってきました

It works!
Amazing!

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

I organize autoracex.
And I take part in NervesJP, fukuoka.ex, EDI, tokyo.ex, Pelemay.