r/adventofcode • • Dec 04 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 4 Solutions -๐ŸŽ„-

--- Day 4: High-Entropy Passphrases ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

18 Upvotes

319 comments sorted by

View all comments

2

u/disclosure5 Dec 04 '17

Late to the party, but Erlang.

1

u/Warbringer007 Dec 04 '17

My Erlang code is totally different :D

first(File) ->
	Input = prepare:func_text(File),
	solveFirst(Input, 0).

solveFirst([], Count) ->
	Count;

solveFirst([H|T], Count) ->
	solveFirst(T, Count + solveRow(H)).

solveRow([]) ->
	1;

solveRow([H|T]) ->
	case lists:member(H, T) of
		true -> 0;
		false -> solveRow(T)
	end.

second(File) ->
	Input = prepare:func_text(File),
	solveSecond(Input, 0).

solveSecond([], Count) ->
	Count;
	
solveSecond([H|T], Count) ->
	solveSecond(T, Count + solveSecondRow(H)).

solveSecondRow([]) ->
	1;

solveSecondRow([H|T]) ->
	case contains_anagram(H, T) of
		true -> 0;
		false -> solveSecondRow(T)
	end.

contains_anagram(_, []) ->
	false;

contains_anagram(First, [H|T]) ->
	Head = [[X] || X <- H],
	FirstList = [[X] || X <- First],
	case is_anagram(FirstList, Head) of
		true -> true;
		false -> contains_anagram(First, T)
	end.

is_anagram([], "") ->
	true;

is_anagram([], _) ->
	false;

is_anagram([H|T], Second) ->
	case lists:member(H, Second) of
		true -> is_anagram(T, lists:delete(H, Second));
		false -> false
	end.

prepare:func_text(File) reads file and stores it into list of lists of strings.