Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(sedmi2018).
- -compile(export_all).
- -import(prepare, []).
- task(File) ->
- In = [[After,Before] || [_, Before, _, _, _, _, _, After, _, _, _, _] <- prepare:func_text(File)],
- SortedIn = lists:sort(fun([A1, _], [A2, _]) -> A1 =< A2 end, In),
- FormattedIn = formatInput(SortedIn, [[X] || X <- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"], [], []),
- io:format("~p~n", [firstTask(FormattedIn, FormattedIn, [])]),
- secondTask(FormattedIn, FormattedIn, ["", 0], ["", 0], ["", 0], ["", 0], ["", 0], 0).
- formatInput(_, [], Total, _) -> lists:reverse(Total);
- formatInput([], [After | TL], Total, Curr) ->
- formatInput([], TL, [[After | [Curr]] | Total], []);
- formatInput([[After, Before] | T], [After | TL], Total, Curr) ->
- formatInput(T, [After | TL], Total, [Before | Curr]);
- formatInput([[After, Before] | T], [HL | TL], Total, Curr) ->
- formatInput([[After, Before] | T], TL, [[HL | [Curr]] | Total], []).
- firstTask([], [], Total) -> Total;
- firstTask([[After, []] | _], FormattedInAll, Total) ->
- CleanedList = cleanup(FormattedInAll, After, []),
- firstTask(CleanedList, CleanedList, Total ++ After);
- firstTask([[_, _] | T], FormattedInAll, Total) ->
- firstTask(T, FormattedInAll, Total).
- secondTask([], [], _, _, _, _, _, N) -> N;
- secondTask([], FormattedInAll, Work1, Work2, Work3, Work4, Work5, N) ->
- [NewFormattedAll, NewWork1, NewWork2, NewWork3, NewWork4, NewWork5, NewN] = burnoutTasks(FormattedInAll, Work1, Work2, Work3, Work4, Work5, N),
- secondTask(NewFormattedAll, NewFormattedAll, NewWork1, NewWork2, NewWork3, NewWork4, NewWork5, NewN);
- secondTask([[After, []] | T], FormattedInAll, Work1, Work2, Work3, Work4, Work5, N) ->
- [Letter1, N1] = Work1,
- [Letter2, N2] = Work2,
- [Letter3, N3] = Work3,
- [Letter4, N4] = Work4,
- [Letter5, N5] = Work5,
- case (After =:= Letter1) or (After =:= Letter2) or (After =:= Letter3) or (After =:= Letter4) or (After =:= Letter5) of
- true -> secondTask(T, FormattedInAll, Work1, Work2, Work3, Work4, Work5, N);
- false -> case N1 < 1 of
- true -> secondTask(T, FormattedInAll, [After, hd(After)-4], Work2, Work3, Work4, Work5, N);
- _ ->
- case N2 < 1 of
- true -> secondTask(T, FormattedInAll, Work1, [After, hd(After)-4], Work3, Work4, Work5, N);
- _ -> case N3 < 1 of
- true -> secondTask(T, FormattedInAll, Work1, Work2, [After, hd(After)-4], Work4, Work5, N);
- _ -> case N4 < 1 of
- true -> secondTask(T, FormattedInAll, Work1, Work2, Work3, [After, hd(After)-4], Work5, N);
- _ -> case N5 < 1 of
- true -> secondTask(T, FormattedInAll, Work1, Work2, Work3, Work4, [After, hd(After)-4], N);
- _ -> [NewFormattedAll, NewWork1, NewWork2, NewWork3, NewWork4, NewWork5, NewN] = burnoutTasks(FormattedInAll, Work1, Work2, Work3, Work4, Work5, N),
- secondTask(NewFormattedAll, NewFormattedAll, NewWork1, NewWork2, NewWork3, NewWork4, NewWork5, NewN)
- end
- end
- end
- end
- end
- end;
- secondTask([[_, _] | T], FormattedInAll, Work1, Work2, Work3, Work4, Work5, N) ->
- secondTask(T, FormattedInAll, Work1, Work2, Work3, Work4, Work5, N).
- cleanup([], _, New) -> lists:reverse(New);
- cleanup([[After, _] | T], After, New) ->
- cleanup(T, After, New);
- cleanup([[After, Before] | T], Letter, New) ->
- cleanup(T, Letter, [[After | [cleanIn(Before, Letter, [])]] | New]).
- cleanIn([], _, Total) -> Total;
- cleanIn([H | T], H, Total) ->
- cleanIn(T, H, Total);
- cleanIn([H | T], Letter, Total) ->
- cleanIn(T, Letter, [H | Total]).
- burnoutTasks(FormattedInAll, Work1, Work2, Work3, Work4, Work5, N) ->
- [Letter1, N1] = Work1,
- [Letter2, N2] = Work2,
- [Letter3, N3] = Work3,
- [Letter4, N4] = Work4,
- [Letter5, N5] = Work5,
- [CleanedList1, NewWork1] = case N1 - 1 of
- 0 -> [cleanup(FormattedInAll, Letter1, []), ["", 0]];
- _ -> [FormattedInAll, [Letter1, N1 - 1]]
- end,
- [CleanedList2, NewWork2] = case N2 - 1 of
- 0 -> [cleanup(CleanedList1, Letter2, []), ["", 0]];
- _ -> [CleanedList1, [Letter2, N2 - 1]]
- end,
- [CleanedList3, NewWork3] = case N3 - 1 of
- 0 -> [cleanup(FormattedInAll, Letter3, []), ["", 0]];
- _ -> [CleanedList2, [Letter3, N3 - 1]]
- end,
- [CleanedList4, NewWork4] = case N4 - 1 of
- 0 -> [cleanup(CleanedList3, Letter4, []), ["", 0]];
- _ -> [CleanedList3, [Letter4, N4 - 1]]
- end,
- [CleanedList5, NewWork5] = case N5 - 1 of
- 0 -> [cleanup(CleanedList4, Letter5, []), ["", 0]];
- _ -> [CleanedList4, [Letter5, N5 - 1]]
- end,
- case FormattedInAll =:= CleanedList5 of
- true -> burnoutTasks(FormattedInAll, NewWork1, NewWork2, NewWork3, NewWork4, NewWork5, N+1);
- false -> [CleanedList5, NewWork1, NewWork2, NewWork3, NewWork4, NewWork5, N+1]
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement