more efficient algo
This commit is contained in:
parent
7c235ccbe5
commit
bff06901be
65
07/part2.pl
65
07/part2.pl
@ -16,56 +16,35 @@ payout([_-Bet | Cdr], Rank, Payout) :-
|
|||||||
|
|
||||||
% score(Hand, Score) gets the score for one hand, that can be directly compared.
|
% score(Hand, Score) gets the score for one hand, that can be directly compared.
|
||||||
score([A, B, C, D, E], Score) :-
|
score([A, B, C, D, E], Score) :-
|
||||||
(
|
type([A, B, C, D, E], Type),
|
||||||
type([A, B, C, D, E], 7) -> Type is 7;
|
|
||||||
type([A, B, C, D, E], 6) -> Type is 6;
|
|
||||||
type([A, B, C, D, E], 5) -> Type is 5;
|
|
||||||
type([A, B, C, D, E], 4) -> Type is 4;
|
|
||||||
type([A, B, C, D, E], 3) -> Type is 3;
|
|
||||||
type([A, B, C, D, E], 2) -> Type is 2;
|
|
||||||
type([A, B, C, D, E], 1) -> Type is 1
|
|
||||||
),
|
|
||||||
Score is E + 100*D + (100**2)*C + (100**3)*B + (100**4)*A + (100**5)*Type.
|
Score is E + 100*D + (100**2)*C + (100**3)*B + (100**4)*A + (100**5)*Type.
|
||||||
|
|
||||||
% type(Hand, Type) get the type rank for one hand, ordered.
|
type([1, 1, 1, 1, 1], 7).
|
||||||
type([A, B, C, D, E], Type) :-
|
type(Hand, 7) :- count_hand(Hand, 5, _).
|
||||||
wild([A, B, C, D, E], [Ax, Bx, Cx, Dx, Ex]),
|
type(Hand, 6) :- count_hand(Hand, 4, _).
|
||||||
sort(0, @=<, [Ax, Bx, Cx, Dx, Ex], Sorted),
|
type(Hand, 5) :- count_hand(Hand, 3, 2).
|
||||||
sorted_type(Sorted, Type).
|
type(Hand, 4) :- count_hand(Hand, 3, 3).
|
||||||
sorted_type([X, X, X, X, X], 7).
|
type(Hand, 3) :- count_hand(Hand, 2, 3).
|
||||||
sorted_type([X, X, X, X, A], 6) :- X =\= A.
|
type(Hand, 2) :- count_hand(Hand, 2, 4).
|
||||||
sorted_type([A, X, X, X, X], 6) :- X =\= A.
|
type(Hand, 1) :- count_hand(Hand, 1, _).
|
||||||
sorted_type([Y, Y, X, X, X], 5) :- X =\= Y.
|
|
||||||
sorted_type([X, X, X, Y, Y], 5) :- X =\= Y.
|
|
||||||
sorted_type([X, X, X, A, B], 4) :- X =\= A, A =\= B.
|
|
||||||
sorted_type([A, X, X, X, B], 4) :- A =\= X, X =\= B.
|
|
||||||
sorted_type([A, B, X, X, X], 4) :- A =\= B, B =\= X.
|
|
||||||
sorted_type([X, X, Y, Y, A], 3) :- X =\= Y, Y =\= A.
|
|
||||||
sorted_type([X, X, A, Y, Y], 3) :- X =\= A, A =\= Y.
|
|
||||||
sorted_type([A, X, X, Y, Y], 3) :- A =\= X, X =\= Y.
|
|
||||||
sorted_type([X, X, A, B, C], 2) :- X =\= A, A =\= B, B =\= C.
|
|
||||||
sorted_type([A, X, X, B, C], 2) :- A =\= X, X =\= B, B =\= C.
|
|
||||||
sorted_type([A, B, X, X, C], 2) :- A =\= B, B =\= X, X =\= C.
|
|
||||||
sorted_type([A, B, C, X, X], 2) :- A =\= B, B =\= C, C =\= X.
|
|
||||||
sorted_type([A, B, C, D, E], 1) :- A =\= B, B =\= C, C =\= D, D =\= E.
|
|
||||||
|
|
||||||
% wild replaces a wildcard with any possible other card
|
count_hand(Hand, NMaxCard, NUniqueCards) :-
|
||||||
wild(_, X, Y) :- card(X), X =\= 1, Y is X.
|
convlist([X, X]>>(X =\= 1), Hand, NoJokerHand),
|
||||||
wild([A, B, C, D, E], X, Y) :-
|
maplist(count(NoJokerHand), NoJokerHand, Counts),
|
||||||
card(X), X =:= 1,
|
max_member(MaxCountNoJoker, Counts),
|
||||||
(Y is A; Y is B; Y is C; Y is D; Y is E), Y =\= 1.
|
count(Hand, 1, NJokers),
|
||||||
wild([1, 1, 1, 1, 1], [2, 2, 2, 2, 2]).
|
NMaxCard is MaxCountNoJoker + NJokers,
|
||||||
wild(Hand, WildHand) :- maplist(wild(Hand), Hand, WildHand).
|
sort(NoJokerHand, Uniques), length(Uniques, NUniqueCards).
|
||||||
|
|
||||||
|
% count(List, Elem, Count), Count = number of occurences of Elem in List.
|
||||||
|
count([], _, 0).
|
||||||
|
count([X | Cdr], X, N) :- count(Cdr, X, NRest), N is NRest + 1.
|
||||||
|
count([Y | Rest], X, N) :- Y =\= X, count(Rest, X, N).
|
||||||
|
|
||||||
% card(Ascii, Value) converts between card ascii value and internal values
|
% card(Ascii, Value) converts between card ascii value and internal values
|
||||||
card(84, 10). % T
|
|
||||||
card(74, 1). % J
|
card(74, 1). % J
|
||||||
card(81, 12). % Q
|
card(84, 10). card(81, 12). card(75, 13). card(65, 14). % TQKA
|
||||||
card(75, 13). % K
|
|
||||||
card(65, 14). % A
|
|
||||||
card(Char, Card) :- Char >= 48, Char =< 57, Card is Char - 48.
|
card(Char, Card) :- Char >= 48, Char =< 57, Card is Char - 48.
|
||||||
card(1). card(2). card(3). card(4). card(5). card(6). card(7). card(8). card(9).
|
|
||||||
card(10). card(12). card(13). card(14).
|
|
||||||
|
|
||||||
% zip 2 lists into a map
|
% zip 2 lists into a map
|
||||||
zip([], [], []).
|
zip([], [], []).
|
||||||
|
Loading…
Reference in New Issue
Block a user