code-shoily
Advent of Code 2021 - Day 3
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
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security










First Post!
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.Most Liked
bjorng
The
~~~operator when given a positive number always returns a negative number. It does that simulate that an integer holds an infinite number of bits. If the number starts with an infinite number of ones, the number is negative. If it starts with an infinite number of zeroes, it is positive.When I first learned Erlang, it took me a while to figure out why that’s make sense.
Yes, an infinite number of bits. Fortunately the runtime system is smart enough to not store all them explicitly.
Aetherus
Again, my solution:
Part 1
Part 2
josevalim
My solution: https://github.com/josevalim/aoc/blob/main/2021/day-03.livemd
Live streaming: Twitch - we also solved part 1 with Nx and had a bit of fun with Livebook.
Last Post!
adamu
I did it with bitstrings. But I wasn’t clever enough to notice you can skip calculating eplison.
Also, yes I know it’s 2022. Had submarine problems.
https://git.adamu.jp/adam/AdventOfCode/src/branch/main/2021/day3.exs