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}")