about summary refs log tree commit diff
path: root/day5.exs
diff options
context:
space:
mode:
authorBen Harris <ben@tilde.team>2018-12-05 02:12:19 -0500
committerBen Harris <ben@tilde.team>2018-12-05 02:12:19 -0500
commitd06708a9a2c0507f3a5581a593cc6435d767ed5e (patch)
tree9c2dcf18cb5a0d6ab1bc48c3dbdf0dfbccf8da83 /day5.exs
parent1fb359f4831f23653c5517f6ef0d1dc0b607db4d (diff)
oops, forgot to push day 4; here's day 5 too HEAD master
Diffstat (limited to 'day5.exs')
-rw-r--r--day5.exs47
1 files changed, 47 insertions, 0 deletions
diff --git a/day5.exs b/day5.exs
new file mode 100644
index 0000000..120f185
--- /dev/null
+++ b/day5.exs
@@ -0,0 +1,47 @@
+defmodule Day5 do
+  def input() do
+    File.stream!("input/day5.in")
+    |> Stream.map(&String.trim/1)
+    |> Enum.at(0)
+  end
+
+  def part1() do
+    input()
+    |> String.graphemes()
+    |> react()
+    |> Enum.count()
+  end
+
+  def part2() do
+    for(n <- ?a..?z, do: <<n::utf8>>)
+    |> Enum.map(fn c ->
+      input()
+      |> String.replace(c, "")
+      |> String.replace(String.upcase(c), "")
+      |> String.graphemes()
+      |> react()
+    end)
+    |> Enum.min_by(&Enum.count/1)
+    |> Enum.count()
+  end
+
+  def react(polymer) do
+    Enum.reduce(polymer, [], fn
+      x, [] ->
+        [x]
+
+      x, [y] ->
+        [x, y]
+
+      x, [y | ys] ->
+        if x != y && String.downcase(x) == String.downcase(y) do
+          ys
+        else
+          [x, y | ys]
+        end
+    end)
+  end
+end
+
+IO.puts(Day5.part1())
+IO.puts(Day5.part2())