Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. split(0, List, [], List).
  2. split(N, [X|List], [X|Init], Tail) :- M is N - 1, split(M, List, Init, Tail).
  3.  
  4. group(_, [], []).
  5. group(N, List, [GroupedInit|GroupedRest]) :- split(N, List, GroupedInit, Rest), group(N, Rest, GroupedRest).
  6.  
  7. each(_, [], []).
  8. each(N, [H|T], [H]) :- length(T, L), L < N - 1.
  9. each(N, List, [H|Rest]) :- split(N, List, [H|_], Tail), each(N, Tail, Rest).
  10.  
  11. input(InputNumbers) :- open('input.txt', read, InputFile),
  12. read_string(InputFile, _, InputString),
  13. split_string(InputString, " \n", " \n", InputList),
  14. maplist(atom_number, InputList, InputNumbers).
  15.  
  16. valid_triangle(A, B, C) :- A + B > C, A + C > B, B + C > A.
  17.  
  18. count_satisfying([], 0).
  19. count_satisfying([H|T], N) :- count_satisfying(T, M), (apply(valid_triangle, H) -> N is M + 1; N is M).
  20.  
  21. part1(N) :- input(Input), group(3, Input, Triangles), count_satisfying(Triangles, N).
  22. part2(N) :- input(Input),
  23. each(3, Input, Left),
  24. split(1, Input, _, Offset1Input), each(3, Offset1Input, Center),
  25. split(2, Input, _, Offset2Input), each(3, Offset2Input, Right),
  26. append(Left, Center, Partial), append(Partial, Right, TransformedInput),
  27. group(3, TransformedInput, Triangles), count_satisfying(Triangles, N).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement