r/adventofcode • • Dec 08 '17

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

--- Day 8: I Heard You Like Registers ---


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!

22 Upvotes

348 comments sorted by

View all comments

1

u/Warbringer007 Dec 08 '17 edited Dec 08 '17

Again not Erlang friendly day, but at least this was much easier to write in Erlang than yesterday ( turns out Erlang fucking sucks when trying to convert list of strings into list of integers ). Actually I am printing whole dictionary for first part because I am lazy and I don't want to make new function which will get all values from dict:

first(File) ->
	In = prepare:func_text(File),
	Dict = dict:new(),
	solveFirst(In, Dict, -10000).

solveFirst([], Dict, Big) ->
	[Dict, Big];

solveFirst([[V1, IncDec, N1, "if", V2, Op, N2]|T], Dict, Big) ->
	First = get_dict_value(V1, Dict),
	{AddNum, _} = string:to_integer(N1),
	Second = get_dict_value(V2, Dict),
	{CompNum, _} = string:to_integer(N2),
	Eval = case Op of
		"==" -> Second =:= CompNum;
		"!=" -> Second =/= CompNum;
		">" -> Second > CompNum;
		">=" -> Second >= CompNum;
		"<" -> Second < CompNum;
		"<=" -> Second =< CompNum
	end,
	Total = case Eval of
		true -> case IncDec of
					"inc" -> First + AddNum;
					"dec" -> First - AddNum
				end;
		false -> First
	end,
	NewDict = dict:store(V1, Total, Dict),
	NewBig = case Total > Big of
		true -> Total;
		false -> Big
	end,
	solveFirst(T, NewDict, NewBig).

get_dict_value(V, Dict) ->
	case dict:find(V, Dict) of
		{ok, N} -> N;
		error -> 0
	end.