Skip to content

Instantly share code, notes, and snippets.

@Jupiterrr
Created September 7, 2014 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jupiterrr/d0c0c7f1d78684c9dd50 to your computer and use it in GitHub Desktop.
Save Jupiterrr/d0c0c7f1d78684c9dd50 to your computer and use it in GitHub Desktop.
%! Data
generation([9, 10, 21, 20, 7, 11, 4, 15, 7, 7, 14, 5, 20, 6, 29, 8, 11, 19, 18, 22, 29, 14, 27, 17, 6, 22, 12, 18, 18, 30]).
overhead([21, 16, 19, 26, 26, 7, 1, 8, 17, 14, 15, 25, 20, 3, 24, 5, 28, 9, 2, 14, 9, 25, 15, 13, 15, 9, 6, 20, 27, 22]).
budget(2912).
%! Helper
zip([], [], []).
zip([], [_|_], []).
zip([_|_], [], []).
zip([X|Xs], [Y|Ys], [X-Y|XYs]) :-
zip(Xs, Ys, XYs).
slice([X|_],1,1,[X]).
slice([X|Xs],1,K,[X|Ys]) :- K > 1,
K1 is K - 1, slice(Xs,1,K1,Ys).
slice([_|Xs],I,K,Ys) :- I > 1,
I1 is I - 1, K1 is K - 1, slice(Xs,I1,K1,Ys).
ziplist(XYs) :-
generation(Xs),
overhead(Ys),
zip(Xs, Ys, XYs).
single_cost(Cards, G-O, X) :- X is G+O*(Cards-1).
cost(_, [], 0).
cost(Cards, [X|Xs], Cost) :-
single_cost(Cards, X, CS),
cost(Cards, Xs, CSub),
Cost is CS + CSub.
result(Cards) :-
ziplist(XYs),
permutation(XYs, Ps),
slice(Ps, 1, Cards, Xs),
cost(Cards, Xs, Cost).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment