3
1

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 1 year has passed since last update.

やすらはで寝なましものをさ夜ふけてかたぶくまでの月を見しかな

Advent Calendar 2022 111日目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 10: Syntax ScoringElixirで楽しんでみます。

スクリーンショット 2022-05-05 21.06.28.png

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

私の回答

私の回答です。

私の回答
input = """
[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]
"""
defmodule Recursion do
  def recur(list) do
    recur(list, [])
  end

  defp scores([], stack) do
    stack
    |> Enum.reverse()
    |> Enum.reduce(0, fn point, acc ->
      5 * acc + point
    end)
  end

  defp scores([?( | tail], stack), do: scores(tail, [1 | stack])
  defp scores([?[ | tail], stack), do: scores(tail, [2 | stack])
  defp scores([?{ | tail], stack), do: scores(tail, [3 | stack])
  defp scores([?< | tail], stack), do: scores(tail, [4 | stack])

  defp recur([], stack), do: {:incorrect, scores(stack, [])}

  defp recur([head | tail], stack) when head in [?(, ?[, ?{, ?<], do: recur(tail, [head | stack])

  defp recur([?) | tail], [?( | stack]), do: recur(tail, stack)

  defp recur([?) | _tail], _stack), do: {:corrupted, 3}

  defp recur([?] | tail], [?[ | stack]), do: recur(tail, stack)

  defp recur([?] | _tail], _stack), do: {:corrupted, 57}

  defp recur([?} | tail], [?{ | stack]), do: recur(tail, stack)

  defp recur([?} | _tail], _stack), do: {:corrupted, 1197}

  defp recur([?> | tail], [?< | stack]), do: recur(tail, stack)

  defp recur([?> | _tail], _stack), do: {:corrupted, 25137}
end

Part 1

input
|> String.split("\n", trim: true)
|> Enum.map(fn line ->
  line
  |> String.to_charlist()
  |> Recursion.recur()
end)
|> Enum.reduce(0, fn
  {:corrupted, score}, acc -> acc + score
  {:incorrect, _score}, acc -> acc
end)
|> IO.inspect()

Part 2

scores =
  input
  |> String.split("\n", trim: true)
  |> Enum.map(fn line ->
    line
    |> String.to_charlist()
    |> Recursion.recur()
  end)
  |> Enum.reduce([], fn
    {:corrupted, _score}, acc -> acc
    {:incorrect, score}, acc -> [score | acc]
  end)
  |> Enum.sort()
  |> IO.inspect()

scores
|> Enum.at(div(Enum.count(scores), 2))
|> IO.inspect()

It works!
Amazing!

お手本

Day 10のお手本(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 10: Syntax ScoringElixirで楽しんでみました。
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」から着想を得て、模倣いたしました。

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?