r/adventofcode • • Dec 13 '17

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

--- Day 13: Packet Scanners ---


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!

16 Upvotes

199 comments sorted by

View all comments

Show parent comment

5

u/Warbringer007 Dec 13 '17 edited Dec 13 '17

Nope, you are not :D. I solved almost every day in Erlang, today was fun:

first(File) ->
	In = prepare:func_text(File),
	L = buildList(In, [], 1),
	io:format("~p~n", [solveFirst(L, 1, 0)]),
	solveSecond(L, 2).

buildList([], L, _) ->
	L;

buildList([H|T], L, N) ->
	{NL, _} = string:to_integer(hd(H)),
	case (NL + 1) =:= N of
		true -> [N2] = tl(H),
				{NL2, _} = string:to_integer(N2),
				buildList(T, L ++ [NL2], N+1);
		false -> buildList([H] ++ T, L ++ [0], N+1)
	end.

solveFirst([], _, Sev) ->
	Sev;

solveFirst([H|T], N, Sev) ->
	case H =:= 0 of
		true -> solveFirst(T, N+1, Sev);
		false -> Numb = (H - 1) * 2,
    			 case N rem Numb =:= 1 of
					true -> solveFirst(T, N+1, Sev + (N-1)* H);
					false -> solveFirst(T, N+1, Sev)
				 end
	end.

solveSecond(L, N) ->
	case solveFirst(L, N, 0) > 0 of
		true -> solveSecond(L, N+1);
		false -> N-1
	end.

prepare:func_text parses input into list of lists, then I add missing zeroes into input list. Idk why I did that, lists:member is a thing :D. Part 2 runs a bit slow, it could be faster if I changed solution of first to stop if severity increases over 0.

1

u/erlangguy Dec 14 '17

Yeah, I considered throwing and catching an exception to abandon once I hit a scanner, but lazy.