From b5707df9eeee2491c192bddb6ff8ae2520672662 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Tue, 10 Sep 2024 17:34:06 +0200 Subject: Day 4 --- 2015/day4.exs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 2015/day4.exs (limited to '2015/day4.exs') 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}") -- cgit v1.2.3