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
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
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
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
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
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
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
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
This topic is about Day 2 of the Advent of Code 2020.
New
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
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
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
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
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
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
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
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...
New
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
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
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
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New







