Advertisement
Guest User

Warbringer007AoC20187th

a guest
Dec 7th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 4.61 KB | None | 0 0
  1. -module(sedmi2018).
  2. -compile(export_all).
  3. -import(prepare, []).
  4.  
  5. task(File) ->
  6.     In = [[After,Before] || [_, Before, _, _, _, _, _, After, _, _, _, _] <- prepare:func_text(File)],
  7.     SortedIn = lists:sort(fun([A1, _], [A2, _]) -> A1 =< A2 end, In),
  8.     FormattedIn = formatInput(SortedIn, [[X] || X <- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"], [], []),
  9.     io:format("~p~n", [firstTask(FormattedIn, FormattedIn, [])]),
  10.     secondTask(FormattedIn, FormattedIn, ["", 0], ["", 0], ["", 0], ["", 0], ["", 0], 0).
  11.  
  12. formatInput(_, [], Total, _) -> lists:reverse(Total);
  13. formatInput([], [After | TL], Total, Curr) ->
  14.     formatInput([], TL, [[After | [Curr]] | Total], []);
  15. formatInput([[After, Before] | T], [After | TL], Total, Curr) ->
  16.     formatInput(T, [After | TL], Total, [Before | Curr]);
  17. formatInput([[After, Before] | T], [HL | TL], Total, Curr) ->
  18.     formatInput([[After, Before] | T], TL, [[HL | [Curr]] | Total], []).
  19.  
  20. firstTask([], [], Total) -> Total;
  21. firstTask([[After, []] | _], FormattedInAll, Total) ->
  22.     CleanedList = cleanup(FormattedInAll, After, []),
  23.     firstTask(CleanedList, CleanedList, Total ++ After);
  24. firstTask([[_, _] | T], FormattedInAll, Total) ->
  25.     firstTask(T, FormattedInAll, Total).
  26.  
  27. secondTask([], [], _, _, _, _, _, N) -> N;
  28. secondTask([], FormattedInAll, Work1, Work2, Work3, Work4, Work5, N) ->
  29.     [NewFormattedAll, NewWork1, NewWork2, NewWork3, NewWork4, NewWork5, NewN] = burnoutTasks(FormattedInAll, Work1, Work2, Work3, Work4, Work5, N),
  30.     secondTask(NewFormattedAll, NewFormattedAll, NewWork1, NewWork2, NewWork3, NewWork4, NewWork5, NewN);
  31. secondTask([[After, []] | T], FormattedInAll, Work1, Work2, Work3, Work4, Work5, N) ->
  32.     [Letter1, N1] = Work1,
  33.     [Letter2, N2] = Work2,
  34.     [Letter3, N3] = Work3,
  35.     [Letter4, N4] = Work4,
  36.     [Letter5, N5] = Work5,
  37.     case (After =:= Letter1) or (After =:= Letter2) or (After =:= Letter3) or (After =:= Letter4) or (After =:= Letter5) of
  38.         true -> secondTask(T, FormattedInAll, Work1, Work2, Work3, Work4, Work5, N);
  39.         false -> case N1 < 1 of
  40.                     true -> secondTask(T, FormattedInAll, [After, hd(After)-4], Work2, Work3, Work4, Work5, N);
  41.                     _ ->
  42.                         case N2 < 1 of
  43.                             true -> secondTask(T, FormattedInAll, Work1, [After, hd(After)-4], Work3, Work4, Work5, N);
  44.                             _ -> case N3 < 1 of
  45.                                     true -> secondTask(T, FormattedInAll, Work1, Work2, [After, hd(After)-4], Work4, Work5, N);
  46.                                     _ -> case N4 < 1 of
  47.                                             true -> secondTask(T, FormattedInAll, Work1, Work2, Work3, [After, hd(After)-4], Work5, N);
  48.                                             _ -> case N5 < 1 of
  49.                                                     true -> secondTask(T, FormattedInAll, Work1, Work2, Work3, Work4, [After, hd(After)-4], N);
  50.                                                     _ -> [NewFormattedAll, NewWork1, NewWork2, NewWork3, NewWork4, NewWork5, NewN] = burnoutTasks(FormattedInAll, Work1, Work2, Work3, Work4, Work5, N),
  51.                                                          secondTask(NewFormattedAll, NewFormattedAll, NewWork1, NewWork2, NewWork3, NewWork4, NewWork5, NewN)
  52.                                                  end
  53.                                          end
  54.                                  end
  55.                         end
  56.                 end
  57.     end;
  58. secondTask([[_, _] | T], FormattedInAll, Work1, Work2, Work3, Work4, Work5, N) ->
  59.     secondTask(T, FormattedInAll, Work1, Work2, Work3, Work4, Work5, N).
  60.  
  61. cleanup([], _, New) -> lists:reverse(New);
  62. cleanup([[After, _] | T], After, New) ->
  63.     cleanup(T, After, New);
  64. cleanup([[After, Before] | T], Letter, New) ->
  65.     cleanup(T, Letter, [[After | [cleanIn(Before, Letter, [])]] | New]).
  66.  
  67. cleanIn([], _, Total) -> Total;
  68. cleanIn([H | T], H, Total) ->
  69.     cleanIn(T, H, Total);
  70. cleanIn([H | T], Letter, Total) ->
  71.     cleanIn(T, Letter, [H | Total]).
  72.  
  73. burnoutTasks(FormattedInAll, Work1, Work2, Work3, Work4, Work5, N) ->
  74.     [Letter1, N1] = Work1,
  75.     [Letter2, N2] = Work2,
  76.     [Letter3, N3] = Work3,
  77.     [Letter4, N4] = Work4,
  78.     [Letter5, N5] = Work5,
  79.     [CleanedList1, NewWork1] = case N1 - 1 of
  80.         0 -> [cleanup(FormattedInAll, Letter1, []), ["", 0]];
  81.         _ -> [FormattedInAll, [Letter1, N1 - 1]]
  82.     end,
  83.     [CleanedList2, NewWork2] = case N2 - 1 of
  84.         0 -> [cleanup(CleanedList1, Letter2, []), ["", 0]];
  85.         _ -> [CleanedList1, [Letter2, N2 - 1]]
  86.     end,
  87.     [CleanedList3, NewWork3] = case N3 - 1 of
  88.         0 -> [cleanup(FormattedInAll, Letter3, []), ["", 0]];
  89.         _ -> [CleanedList2, [Letter3, N3 - 1]]
  90.     end,
  91.     [CleanedList4, NewWork4] = case N4 - 1 of
  92.         0 -> [cleanup(CleanedList3, Letter4, []), ["", 0]];
  93.         _ -> [CleanedList3, [Letter4, N4 - 1]]
  94.     end,
  95.     [CleanedList5, NewWork5] = case N5 - 1 of
  96.         0 -> [cleanup(CleanedList4, Letter5, []), ["", 0]];
  97.         _ -> [CleanedList4, [Letter5, N5 - 1]]
  98.     end,
  99.     case FormattedInAll =:= CleanedList5 of
  100.         true -> burnoutTasks(FormattedInAll, NewWork1, NewWork2, NewWork3, NewWork4, NewWork5, N+1);
  101.         false -> [CleanedList5, NewWork1, NewWork2, NewWork3, NewWork4, NewWork5, N+1]
  102.     end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement