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