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
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
#!/usr/bin/env elixir
use Bitwise
max = ((1 <<< 12) - 1)
gamma =
File.stream!("input.txt")
|> Stream.map(&String.trim/1)
|> Stream.map(&String.split(&1, "", trim: true))
|> Enum.zip()
|> Enum.map(&Tuple.to_list/1)
|> Stream.map(&Enum.frequencies/1)
|> Stream.map(&Enum.max_by(&1, fn {_, f} -> f end))
|> Stream.map(&elem(&1, 0))
|> Enum.join("")
|> String.to_integer(2)
epsilon = max - gamma
IO.inspect(epsilon * gamma)
Part 2
#!/usr/bin/env elixir
defmodule P2 do
def o2([elem], _pos), do: to_i(elem)
def o2(list, pos) do
groups = Enum.group_by(list, &elem(&1, pos))
group0 = groups["0"] || []
group1 = groups["1"] || []
if length(group0) > length(group1) do
o2(group0, pos + 1)
else
o2(group1, pos + 1)
end
end
def co2([elem], _pos), do: to_i(elem)
def co2(list, pos) do
groups = Enum.group_by(list, &elem(&1, pos))
group0 = groups["0"] || []
group1 = groups["1"] || []
if length(group1) < length(group0) do
co2(group1, pos + 1)
else
co2(group0, pos + 1)
end
end
defp to_i(tuple), do:
tuple
|> Tuple.to_list()
|> Enum.join("")
|> String.to_integer(2)
end
input =
File.stream!("input.txt")
|> Stream.map(&String.trim/1)
|> Stream.map(&String.split(&1, "", trim: true))
|> Enum.map(&List.to_tuple/1)
IO.inspect(P2.o2(input, 0) * P2.co2(input, 0))
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. ![]()
Popular in Challenges
Other popular topics
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
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








