4
0

More than 1 year has passed since last update.

Advent Of Code 2021 (Day 7: The Treachery of Whales)をElixirで楽しむ

Last updated at Posted at 2022-04-24

あらざらむこの世のほかの思ひ出に今ひとたぴのあふこともがな

I'm looking forward to 12/25,2022

はじめに

この記事は、Advent Of Code 2021 Day 7: The Treachery of WhalesElixirで楽しんでみます。

私の回答

``````input = "3,4,3,1,2"
``````

Part 1

``````list = input |> String.split(",", trim: true) |> Enum.map(&String.to_integer/1)

count = Enum.count(list)
max = Enum.max(list)

0..max |> Enum.reduce(count * max, fn pos, min_fuel ->
{largers, smallers} = Enum.split_with(list, & &1 >= pos)
fuel = Enum.sum(largers) - pos * Enum.count(largers) + pos * Enum.count(smallers) - Enum.sum(smallers)
if fuel < min_fuel, do: fuel, else: min_fuel
end)
``````

Part 2

``````0..max |> Enum.reduce(Enum.sum(1..max) * count, fn pos, min_fuel ->
fuel = Enum.reduce(list, 0, fn i, acc -> Enum.sum(0..abs(i - pos)) + acc end)
if fuel < min_fuel, do: fuel, else: min_fuel
end)
``````

It works!
Amazing!

お手本

Day 7のお手本(José Valimさんの動画)はありませんでした

Day 8はまた再びありました。

Wrapping up

Advent Of Code 2021 Day 7: The Treachery of WhalesElixirで楽しんでみました。
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.

We Are The Alchemists, my friends!

4
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
4
0