rugyoga
Most Liked
qhwa
Solved with a genetic algorithm. 
4
wasi0013
Easy one! 
y2021/day_07.ex
2
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
2
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
2
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
2
Popular in Challenges
This topic is about Day 15 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/l...
New
Here is my solution for day 4:
New
New
Not the prettiest but it works
New
Hello, guys. I’m back again, but only for the weekends, maybe.
This topic is about Day 13 of the Advent of Code 2020 .
Thanks to @egze,...
New
It is that time of the year again: Advent of Code 2022 :christmas_tree:
Day 1
Leaderboard:
New
Today’s problem is really tense. I don’t think I can do it without libgraph.
New
Probably not the most efficient implementation, because part 1 took >1 ms and part 2 >4ms, but the code was simple enough.
def p...
New
Note by the Moderators: This topic is to talk about Day 6 of the Advent of Code.
For general discussion about the Advent of Code 2018 an...
New
Since I started using Elixir, I have benefited greatly from being able to study various open-source projects. The codebase of LiveBook, i...
New
Other popular topics
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
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
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including.
What is Phoenix LiveV...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New







