blob: 1e437ad3a3d895673622babd6f6ee69d0c8e26ec (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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}")
|