r/adventofcode • (AoC creator) • Dec 12 '17

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

--- Day 12: Digital Plumber ---


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!

14 Upvotes

233 comments sorted by

View all comments

1

u/Warbringer007 Dec 12 '17

Erlang, quite lengthy but straightforward, func_text will have list of lists of inputs in this format: ["0", "1352", "1864"] where 0 is first element and every other element is connected to it:

first(File) ->
	In = prepare:func_text(File),
	First = solveFirst(In, ["0"], 1),
	Groups = [First],
	AllGroups = solveSecond(In, Groups, In),
	{length(hd(AllGroups)), length(AllGroups)}.

solveFirst(In, List, N) ->
	{NNumber, _} = string:to_integer(lists:nth(N, List)),
	[_|Others] = lists:nth(NNumber+1, In),
	NewList = fillList(List, Others),
	case length(NewList) =:= N of
		true -> NewList;
		false -> solveFirst(In, NewList, N+1)
	end.

fillList(T, []) ->
	T;

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

solveSecond([], Groups, _) ->
	Groups;

solveSecond([H|T], Groups, In) ->
	case memberOfGroups(hd(H), Groups) of
		true -> solveSecond(T, Groups, In);
		false -> NewGroup = solveFirst(In, [hd(H)], 1),
				 solveSecond(T, Groups ++ [NewGroup], In)
	end.

memberOfGroups(_, []) ->
	false;

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