about summary refs log tree commit diff
path: root/day2.exs
diff options
context:
space:
mode:
Diffstat (limited to 'day2.exs')
-rw-r--r--day2.exs40
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()