summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2015/day4.exs20
1 files changed, 20 insertions, 0 deletions
diff --git a/2015/day4.exs b/2015/day4.exs
new file mode 100644
index 0000000..1e437ad
--- /dev/null
+++ b/2015/day4.exs
@@ -0,0 +1,20 @@
+code = String.trim(File.read!("inputs/day4.txt"))
+
+find_the_offset = fn regex ->
+ {offset, _} =
+ 0..1_000_000_000_000_000
+ |> Stream.map(fn offset ->
+ newcode = "#{code}#{offset}"
+ hash = :crypto.hash(:md5, newcode) |> Base.encode16()
+ has_the_zeroes = String.match?(hash, regex)
+ {offset, has_the_zeroes}
+ end)
+ |> Enum.find(fn {_, has_the_zeroes} -> has_the_zeroes end)
+ offset
+end
+
+five_zero_offset = find_the_offset.(~r/^0{5}/)
+IO.puts("Five zero offset: #{five_zero_offset}")
+
+six_zero_offset = find_the_offset.(~r/^0{6}/)
+IO.puts("Six zero offset: #{six_zero_offset}")