r/adventofcode • • Dec 06 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 6 Solutions -🎄-

--- Day 6: Chronal Coordinates ---


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.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 6

Transcript:

Rules for raising a programmer: never feed it after midnight, never get it wet, and never give it ___.


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 at 0:26:52!

32 Upvotes

387 comments sorted by

View all comments

1

u/Warbringer007 Dec 06 '18 edited Dec 06 '18

Erlang is very good for matrix and for style tasks, code here ( it is very unreadable, explanation below ):

task(File) ->
	In = prepare:six2018prepare(File),
	[Top, Bottom, Left, Right] = dimensions(In, 10000, 0, 10000, 0),
	firstTask(In, Top-1, Left-1, Top-1, Left-1, Bottom+1, Right+1, 0).

dimensions([], Top, Bottom, Left, Right) -> [Top, Bottom, Left, Right];
dimensions([[HW, HH, _, _] | T], Top, Bottom, Left, Right) ->
	NewTop = case list_to_integer(HH) < Top of
		true -> list_to_integer(HH);
		false -> Top
	end,
	NewBottom = case list_to_integer(HH) > Bottom of
		true -> list_to_integer(HH);
		false -> Bottom
	end,
	NewLeft = case list_to_integer(HW) < Left of
		true -> list_to_integer(HW);
		false -> Left
	end,
	NewRight = case list_to_integer(HW) > Right of
		true -> list_to_integer(HW);
		false -> Right
	end,
	dimensions(T, NewTop, NewBottom, NewLeft, NewRight).

firstTask(In, Bottom, Right, _, _, Bottom, Right, TenK) ->
	{largestNonInfinite(In, 0, 0, 1), TenK};
firstTask(In, Y, Right, Top, Left, Bottom, Right, TenK) ->
	[Coord, Total, Lower] = calcClosest(In, Y, Right, 10000, 0, 0, 1, 0),
	case Total of
		1 -> [W, H, _, Total1] = lists:nth(Coord, In),
			 NewIn = lists:sublist(In, Coord-1) ++ [[W, H, 1, Total1+1]] ++ lists:sublist(In, Coord+1, length(In) - Coord),
			 firstTask(NewIn, Y+1, Left, Top, Left, Bottom, Right, TenK + Lower);
		_ -> firstTask(In, Y+1, Left, Top, Left, Bottom, Right, TenK + Lower)
	end;
firstTask(In, Y, X, Top, Left, Bottom, Right, TenK) ->
	[Coord, Total, Lower] = calcClosest(In, Y, X, 10000, 0, 0, 1, 0),
	case Total of
		1 -> [W, H, Condition, Total1] = lists:nth(Coord, In),
			 NewIn = case (Y =:= Top) or (Y =:= Bottom) or (X =:= Left) or (X =:= Right) of
				true -> lists:sublist(In, Coord-1) ++ [[W, H, 1, Total1+1]] ++ lists:sublist(In, Coord+1, length(In) - Coord);
				false -> lists:sublist(In, Coord-1) ++ [[W, H, Condition, Total1+1]] ++ lists:sublist(In, Coord+1, length(In) - Coord)
			 end,
			 firstTask(NewIn, Y, X+1, Top, Left, Bottom, Right, TenK + Lower);
		_ -> firstTask(In, Y, X+1, Top, Left, Bottom, Right, TenK + Lower)
	end.

calcClosest([], _, _, _, Coord, Total, _, Sum) ->
	case Sum < 10000 of
		true -> [Coord, Total, 1];
		false -> [Coord, Total, 0]
	end;
calcClosest([[HW, HH, _, _] | T], Y, X, Distance, Coord, Total, N, Sum) ->
	ManDist = abs(list_to_integer(HW) - X) + abs(list_to_integer(HH) - Y),
	case ManDist < Distance of
		true -> calcClosest(T, Y, X, ManDist, N, 1, N+1, Sum + ManDist);
		false -> case ManDist =:= Distance of
			true -> calcClosest(T, Y, X, Distance, Coord, Total + 1, N+1, Sum + ManDist);
			false ->  calcClosest(T, Y, X, Distance, Coord, Total, N+1, Sum + ManDist)
		end
	end.

largestNonInfinite([], Total, _, _) -> Total;
largestNonInfinite([[_, _, 1, _] | T], Total, Coord, N) -> largestNonInfinite(T, Total, Coord, N+1);
largestNonInfinite([[_, _, 0, Count] | T], Total, Coord, N) ->
	case Count > Total of
		true -> largestNonInfinite(T, Count, N, N+1);
		false -> largestNonInfinite(T, Total, Coord, N+1)
	end.

I converted each row into [X, Y, IsInfinite, CountClosest] type of list, then I calculated dimensions of matrix. After that I simply proceeded with problem ( which is not simple in programming language without for command ), however I had to track if I'm at the edge. If some coordinate is closest to the edge, it is infinite, therefore it doesn't count for first part of the problem. TenK variable tracks how big is the region for second part of the problem.