Skip to content

Instantly share code, notes, and snippets.

@chriserik
Created March 21, 2019 23:11
Show Gist options
  • Save chriserik/54062fca9a847c92ae8d77641eb93a2d to your computer and use it in GitHub Desktop.
Save chriserik/54062fca9a847c92ae8d77641eb93a2d to your computer and use it in GitHub Desktop.
Day 2 Challenge Advent of Code
lines = File.read!("./input.txt") |> String.trim() |> String.split("\n")
defmodule Day2 do
  def count_occurrences(line) do
    String.split(line, "")
    |> Enum.filter(fn x -> x != "" end)
    |> group_and_count()
    |> Enum.uniq()
  end
  def group_and_count(list) do
    list
    |> Enum.group_by(fn x -> x end)
    |> Enum.filter(fn {k, v} -> Enum.count(v) > 1 end)
    |> Enum.map(fn {char, occurrences} -> Enum.count(occurrences) end)
  end
end
lines
|> Enum.map(&Day2.count_occurrences/1)
|> Enum.concat()
|> Day2.group_and_count()
|> Enum.reduce(fn v, acc -> acc * v end)
|> IO.inspect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment