rugyoga

rugyoga

Advent of Code 2021 - Day 7

part 1

part 2

Most Liked

qhwa

qhwa

Solved with a genetic algorithm. :smiley:

wasi0013

wasi0013

Easy one! :smiley:
y2021/day_07.ex

code-shoily

code-shoily

Not sure if this was accidentally swapped with Day 3 or something. Way too easy.

defmodule AdventOfCode.Y2021.Day07 do
  use AdventOfCode.Helpers.InputReader, year: 2021, day: 7

  def run_1, do: input!() |> parse() |> alignments() |> Enum.min()
  def run_2, do: input!() |> parse() |> alignments(&cost/1) |> Enum.min()
  def parse(data), do: data |> String.split(",") |> Enum.map(&String.to_integer/1)

  defp alignments(positions, cost_fn \\ &Function.identity/1) do
    for i <- positions do
      Enum.sum(for j <- positions, do: cost_fn.(abs(i - j)))
    end
  end

  defp cost(steps), do: (steps == 0 && 0) || div(steps * (steps + 1), 2)
end
epilgrim

epilgrim

hey, this looks so simple, that I had to try it.
Sadly, doesn’t seem to work for my inputs. Could it be that your solution only considers positions that appear in the input file? but the minimum cost might not be one of the original positions. It might be that every submarine needs to be moved

$ elixir day_7.ex
"me"
Part 1: fuel: 342730
Part 2: position: 476
Part 2: fuel: 92335207
"code-shoily"
Part 1: fuel: 342730
Part 2: fuel: 92338199
moogle19

moogle19

My Day 7:

defmodule Day7 do
  def part1(input) do
    input =
      input
      |> parse()
      |> Enum.sort()

    count = Enum.count(input)
    median = Enum.at(input, div(count, 2))

    Enum.reduce(input, 0, fn elem, acc ->
      abs(elem - median) + acc
    end)
  end

  def part2(input) do
    input = parse(input)

    avg = Enum.sum(input) / Enum.count(input)

    [floor(avg), ceil(avg)]
    |> Enum.map(fn cheapest ->
      Enum.reduce(input, 0, fn elem, acc ->
        n = abs(elem - cheapest)
        acc + div(n * (n + 1), 2)
      end)
    end)
    |> Enum.min()
  end

  defp parse(input) do
    input
    |> String.split(",")
    |> Enum.map(&String.to_integer/1)
  end
end

Where Next?

Popular in Challenges Top

lud
Gosh this one took me sooo much time. At first I was trying to iterate each digit independently on the input A number to make digits cha...
New
bjorng
This topic is about Day 17 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/l...
New
JEG2
Note: This topic is to talk about Day 9 of the Advent of Code. For general discussion about the Advent of Code 2018 and links to topics ...
New
antoine-duchenet
Everything went smoothly today. Nothing to change to solve part 2 because I already used memoization for part 1 (it looked like an AoC e...
New
kwando
Phew, this one took a while to get right. My naive attempts was way to slow so I reached for Dijkstras shortest path algorithm… and that ...
New
bjorng
Note: This topic is to talk about Day 6 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can join...
New
cblavier
Hey there :wave: No magic or algorithmic finesse today, I just finished the challenge and I my code is quite slow (1sec for part1, 3se...
New
New
bjorng
Note: This topic is to talk about Day 4 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can join...
New
code-shoily
Here’s my day 3 code This was quite easy. I was afraid Part 2 would be “un-regex-able” and was preparing for hand crafting automata bu...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement