diff options
author | Ben Harris <ben@tilde.team> | 2018-12-02 03:24:52 -0500 |
---|---|---|
committer | Ben Harris <ben@tilde.team> | 2018-12-02 03:24:52 -0500 |
commit | ddcb1277614b912a48ed3be751fba6802fafb55b (patch) | |
tree | 6048f4011d8a069722d7ed585dedb1bed25d8203 /day2.exs | |
parent | b3c1536aa9795c6a9f4c927fb34d57d181fd2c9e (diff) |
day 2
Diffstat (limited to 'day2.exs')
-rw-r--r-- | day2.exs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/day2.exs b/day2.exs new file mode 100644 index 0000000..620ba5a --- /dev/null +++ b/day2.exs @@ -0,0 +1,40 @@ +defmodule Day2 do + def get_lines() do + File.stream!("day2.in") + |> Stream.map(&String.trim/1) + end + + def find_subcount(count) do + get_lines() + |> Enum.reduce(0, fn x, acc -> + if x + |> String.graphemes() + |> Enum.sort() + |> Enum.uniq() + |> Enum.any?(fn y -> + Enum.count(String.graphemes(x), &(&1 == y)) == count + end) do + acc + 1 + else + acc + end + end) + end + + def find_common_chars() do + get_lines() + |> Enum.reduce(%MapSet{}, fn x, acc -> + x + |> String.graphemes() + |> Enum.with_index() + |> Enum.reduce(acc, fn {_c, i}, seen -> + pair = {i, String.slice(x, 0, i) <> String.slice(x, (i + 1)..-1)} + if MapSet.member?(seen, pair), do: IO.puts(elem(pair, 1)) + MapSet.put(seen, pair) + end) + end) + end +end + +IO.puts(Day2.find_subcount(2) * Day2.find_subcount(3)) +Day2.find_common_chars() |