diff options
author | Justin Wernick <j.wernick@eyeo.com> | 2024-09-10 17:34:06 +0200 |
---|---|---|
committer | Justin Wernick <j.wernick@eyeo.com> | 2024-09-10 17:34:06 +0200 |
commit | b5707df9eeee2491c192bddb6ff8ae2520672662 (patch) | |
tree | 56ae309117c97a5e67456d8add58e9ae761be3ee /2015/day4.exs | |
parent | c254f18c0d5bfd2a7713039fcaa36b56d2b26e79 (diff) |
Day 4
Diffstat (limited to '2015/day4.exs')
-rw-r--r-- | 2015/day4.exs | 20 |
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}") |