Advent of Code Solutions
advent-of-code aoc
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

2015: Day 4

yemou 750f9f44 8b989713

+45
+2
2015/04/part1-tests/expected
··· 1 + 609043 2 + 1048970
+1
2015/04/part1-tests/input1
··· 1 + abcdef
+1
2015/04/part1-tests/input2
··· 1 + pqrstuv
+41
2015/04/solution.erl
··· 1 + #!/usr/bin/env escript 2 + 3 + % --- Part 1 Code 4 + part1(Input) -> 5 + io:format("Part 1: ~b~n", [find_hash_with_prefix("00000", Input)]). 6 + 7 + find_hash_with_prefix(PrefixMatch, Input) -> 8 + find_hash_with_prefix(PrefixMatch, Input, 0). 9 + find_hash_with_prefix(PrefixMatch, Input, CurrentNumber) -> 10 + Hash = binary:encode_hex(crypto:hash(md5, [Input, integer_to_list(CurrentNumber)])), 11 + Prefix = string:slice(binary_to_list(Hash), 0, length(PrefixMatch)), 12 + 13 + if 14 + Prefix == PrefixMatch -> CurrentNumber; 15 + true -> find_hash_with_prefix(PrefixMatch, Input, CurrentNumber + 1) 16 + end. 17 + 18 + % --- Part 2 Code 19 + part2(Input) -> 20 + io:format("Part 2: ~b~n", [find_hash_with_prefix("000000", Input)]). 21 + 22 + % --- Generic Helper Functions 23 + read_input() -> read_input(""). 24 + read_input(PrevLine) -> 25 + case io:fread("", "~s") of 26 + eof -> PrevLine; 27 + {error, _} -> halt(1); 28 + {ok, Terms} -> read_input(PrevLine ++ Terms) 29 + end. 30 + 31 + main(Args) -> 32 + Input = read_input(), 33 + case Args of 34 + ["1"] -> 35 + part1(Input); 36 + ["2"] -> 37 + part2(Input); 38 + _ -> 39 + part1(Input), 40 + part2(Input) 41 + end.