Created
March 21, 2019 23:11
-
-
Save chriserik/54062fca9a847c92ae8d77641eb93a2d to your computer and use it in GitHub Desktop.
Day 2 Challenge Advent of Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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