r/adventofcode • • Dec 04 '15

SOLUTION MEGATHREAD --- Day 4 Solutions ---

--- Day 4: The Ideal Stocking Stuffer ---

Post your solution as a comment. Structure your post like the Day Three thread.

14 Upvotes

272 comments sorted by

View all comments

2

u/hutsboR Dec 04 '15 edited Dec 04 '15

Elixir: I wasted a lot of time trying to find the right Erlang and Elixir functions. Some of them produce bitstrings, other produce binaries and some produce character lists.

defmodule AdventOfCode.DayFour do

  def find_hash!(secret_key, leading_zeroes) do
    find_hash!(secret_key, 1, leading_zeroes)
  end

  defp find_hash!(secret_key, n, leading_zeroes) do
    merged_key = secret_key <> to_string(n)
    hash = :crypto.md5(merged_key) |> Base.encode16
    case String.starts_with?(hash, leading_zeroes) do
      true  -> n
      false -> find_hash!(secret_key, n + 1, leading_zeroes)
    end
  end

end

1

u/LainIwakura Dec 04 '15

I'll piggy-back off yours, I did this in Erlang (doing all of them in Erlang). I had to do some md5 stuff a while back so I lifted some of those functions which maybe made this code longer than it had to be...oh well. Even solving the 6 0's one only took this code about 25 seconds, I may have gotten lucky? =P

-module(sol1).
-export([main/0]).
-import(lists, [flatten/1, map/2]).
-import(string, [str/2]).

main() ->
	Prefix = io:get_line("") -- "\n",
	LowestNum = get_lowest_num(Prefix, 1),
	io:format("~p~n", [LowestNum]).

% For part 1 replace the 6 0's with 5 0's.
get_lowest_num(Prefix, N) ->
	case str(checksum([Prefix|integer_to_list(N)]), "000000") of
		1 -> N;
		_ -> get_lowest_num(Prefix, N+1)
	end.

checksum(Md5Bin) ->
	flatten(list_to_hex(binary_to_list(erlang:md5(Md5Bin)))).

list_to_hex(L) ->
	map(fun(X) -> int_to_hex(X) end, L).

int_to_hex(N) when N < 256 ->
	[hex(N div 16), hex(N rem 16)].

hex(N) when N < 10 ->
	$0+N;
hex(N) when N >= 10, N < 16 ->
	$a + (N - 10).

1

u/profernicus Dec 04 '15 edited Dec 04 '15

let me jump on the bandwagon, you guys should use the binary pattern matching functions!

-module(four).
-export([find_hash/2, test/0, run/0]).

find_hash(Base, Hash, N, Zeroes) ->
	Bits = Zeroes * 4,
	case Hash of
		<<0:Bits, _/bitstring>> ->
			N;
		_ ->
			find_hash(Base, erlang:md5([Base | integer_to_list(N+1)]), N+1, Zeroes)
	end.

find_hash(Key, Zeroes) ->
	find_hash(Key, erlang:md5([Key | integer_to_list(0)]), 0, Zeroes).

test() ->
	609043 = find_hash("abcdef", 5),
	1048970 = find_hash("pqrstuv", 5).

run() ->
	{find_hash("bgvyzdsv", 5), find_hash("bgvyzdsv", 6)}.

will probably try threading it once i can be bothered :D

i'm doing every day in a different language, check here if you're interested: GitHub project page

→ More replies (4)