code-shoily
Just did part 1. Part 2 seems to be demanding too much of my reading time so will get to that after I am done with some chores.
Oh here is the repository just in case someone wants the template generator.
I just did the dumbest path - read > transpose each > get most and least common > multiply
I am sure folks will come up with super smart solutions any time now ![]()
defmodule AdventOfCode.Y2021.Day03 do
@moduledoc """
--- Day 3: Binary Diagnostic ---
Problem Link: https://adventofcode.com/2021/day/3
"""
use AdventOfCode.Helpers.InputReader, year: 2021, day: 3
def run_1 do
input!()
|> parse()
|> transpose()
|> bit_frequencies()
|> get_min_max()
|> Tuple.product()
end
def run_2, do: {:not_implemented, 2}
def parse(data), do: data |> String.split("\n", trim: true) |> Enum.map(&String.graphemes/1)
defp transpose(data), do: data |> Enum.zip() |> Enum.map(&Tuple.to_list/1)
defp bit_frequencies(data) do
data
|> Enum.map(&Enum.frequencies/1)
|> Enum.reduce([], fn
%{"0" => lo, "1" => hi}, acc when lo > hi -> [{0, 1} | acc]
_, acc -> [{1, 0} | acc]
end)
end
defp to_integer_by(encoded_data, index) do
encoded_data
|> Enum.map_join(&elem(&1, index))
|> String.reverse()
|> String.to_integer(2)
end
defp get_min_max(encoded_data) do
{to_integer_by(encoded_data, 0), to_integer_by(encoded_data, 1)}
end
end
Trending in Challenges
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Aetherus
There’s a little bit faster solution for part 1, you only need to calculate
gammaorepsilon, not both.Suppose we calculated
gamma, thenepsilonis just(~~~gamma) &&& 0b111111111111, or(1 <<< 12) - 1 - gamma.Aetherus
Again, my solution:
Part 1
Part 2
bjorng
Here is my solution:
https://github.com/bjorng/advent-of-code-2021/blob/b011b56f37056af67581df31a5ae6ab38fb82c23/day03/lib/day03.ex
code-shoily
Took me longer than I’d expected for part 2, although it literally had recursion all over it. I misunderstood the question and thought the frequency map was constant (instead of being recomputed per filter).
Anyways, here’s the second part:
code-shoily
A rough idea, is there a way we can mathematics our way our of Part 2? Like maybe, get the frequencies based on whether the base 10 converted numbers are odd or even?Update: Naah, that would create more nuance than simple.
deadbeef
Feel my solution is kind of ugly; however, I think I took a different approach to determine most common bits, so figured I’d post.
I reduced each bit sequence into a list of the same size. If a bit was
1, increment its position; if0, decrement. At the end, if the value is> 0, most common was1,< 0means0, and0is a tie, represented by-1.So think my recursion in part 2 is similar to others, I just used
Stream.unfold.groovyda
– edited after hauleth’s suggestion below, and also made a single function for the recursion
Here’s mine for Part 2 - didn’t write any function for Part1 - just chained some Enum functions together.
hauleth
Is slower way of writing:
My solution, again as a LiveBook:
Day 3
Input
Task 1
Task 2
groovyda
Thanks for the feedback!
hauleth
Just remember that
length/1is linear wrt the length of the list passed as an argument. Pattern matching against[elem]is constant.