Compare commits
No commits in common. "b2723de03c642919545e272bf07e7e21577641dc" and "ccf63463ffb5a9a38363ba90f9a1514d6e50a69c" have entirely different histories.
b2723de03c
...
ccf63463ff
89
07/part2.pl
89
07/part2.pl
@ -16,40 +16,50 @@ 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),
|
max_type([A, B, C, D, E], Type),
|
||||||
Score is E + 100*D + (100**2)*C + (100**3)*B + (100**4)*A + (100**5)*Type.
|
tiebreaker([A, B, C, D, E], Tiebreaker),
|
||||||
|
Score is Tiebreaker + (100**5)*Type.
|
||||||
|
|
||||||
% type(Hand, MaxCount, Type)
|
tiebreaker([A, B, C, D, E], Score) :-
|
||||||
type([1, 1, 1, 1, 1], 7).
|
Score is E + 100*D + (100**2)*C + (100**3)*B + (100**4)*A.
|
||||||
type(Hand, 7) :- count_hand(Hand, 5, _).
|
|
||||||
type(Hand, 6) :- count_hand(Hand, 4, _).
|
|
||||||
type(Hand, 5) :- count_hand(Hand, 3, 2).
|
|
||||||
type(Hand, 4) :- count_hand(Hand, 3, 3).
|
|
||||||
type(Hand, 3) :- count_hand(Hand, 2, 3).
|
|
||||||
type(Hand, 2) :- count_hand(Hand, 2, 4).
|
|
||||||
type(Hand, 1) :- count_hand(Hand, 1, _).
|
|
||||||
|
|
||||||
count_hand(Hand, NMaxCard, NUniqueCards) :-
|
% max_type gets the max possible type from a hand including wildcards
|
||||||
convlist([X, X]>>(X =\= 1), Hand, NoJokerHand),
|
max_type([A, B, C, D, E], Type) :-
|
||||||
maplist(count(NoJokerHand), NoJokerHand, Counts),
|
(
|
||||||
max_member(MaxCountNoJoker, Counts),
|
type([A, B, C, D, E], 7) -> Type is 7;
|
||||||
count(Hand, 1, NJokers),
|
type([A, B, C, D, E], 6) -> Type is 6;
|
||||||
NMaxCard is MaxCountNoJoker + NJokers,
|
type([A, B, C, D, E], 5) -> Type is 5;
|
||||||
sort(NoJokerHand, Uniques), length(Uniques, NUniqueCards).
|
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
|
||||||
|
).
|
||||||
|
|
||||||
% count(List, Elem, Count), Count = number of occurences of Elem in List.
|
% type(Hand, Type) get the type rank for one hand, ordered.
|
||||||
count([], _, 0).
|
type([A, B, C, D, E], Type) :-
|
||||||
count([X | Cdr], X, N) :- count(Cdr, X, NRest), N is NRest + 1.
|
wild(A, [A, B, C, D, E], Ax),
|
||||||
count([Y | Rest], X, N) :- Y =\= X, count(Rest, X, N).
|
wild(B, [A, B, C, D, E], Bx),
|
||||||
|
wild(C, [A, B, C, D, E], Cx),
|
||||||
% card(Ascii, Value) converts between card ascii value and internal values
|
wild(D, [A, B, C, D, E], Dx),
|
||||||
card(74, 1). % J
|
wild(E, [A, B, C, D, E], Ex),
|
||||||
card(84, 10). card(81, 12). card(75, 13). card(65, 14). % TQKA
|
sort(0, @=<, [Ax, Bx, Cx, Dx, Ex], Sorted),
|
||||||
card(Char, Card) :- Char >= 48, Char =< 57, Card is Char - 48.
|
sorted_type(Sorted, Type).
|
||||||
|
sorted_type([X, X, X, X, X], 7).
|
||||||
% zip 2 lists into a map
|
sorted_type([X, X, X, X, A], 6) :- X =\= A.
|
||||||
zip([], [], []).
|
sorted_type([A, X, X, X, X], 6) :- X =\= A.
|
||||||
zip([A | ACdr], [B | BCdr], [A-B | ABCdr]) :- zip(ACdr, BCdr, ABCdr).
|
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.
|
||||||
|
|
||||||
% convert_input(InputList, Cards, Bets)
|
% convert_input(InputList, Cards, Bets)
|
||||||
convert_input([], [], []).
|
convert_input([], [], []).
|
||||||
@ -59,3 +69,20 @@ convert_input([[Cards, Bet] | Cdr],
|
|||||||
string_to_list(Cards, [A1, A2, A3, A4, A5]),
|
string_to_list(Cards, [A1, A2, A3, A4, A5]),
|
||||||
card(A1, C1), card(A2, C2), card(A3, C3), card(A4, C4), card(A5, C5),
|
card(A1, C1), card(A2, C2), card(A3, C3), card(A4, C4), card(A5, C5),
|
||||||
convert_input(Cdr, HandCdr, BetCdr).
|
convert_input(Cdr, HandCdr, BetCdr).
|
||||||
|
|
||||||
|
wild(X, _, Y) :- X =\= 1, Y is X.
|
||||||
|
wild(X, [A, B, C, D, E], Y) :-
|
||||||
|
X =:= 1,
|
||||||
|
(Y is A; Y is B; Y is C; Y is D; Y is E).
|
||||||
|
|
||||||
|
% card(Ascii, Value) converts between card ascii value and internal values
|
||||||
|
card(84, 10). % T
|
||||||
|
card(74, 1). % J
|
||||||
|
card(81, 12). % Q
|
||||||
|
card(75, 13). % K
|
||||||
|
card(65, 14). % A
|
||||||
|
card(Char, Card) :- Char >= 48, Char =< 57, Card is Char - 48.
|
||||||
|
|
||||||
|
% zip 2 lists into a map
|
||||||
|
zip([], [], []).
|
||||||
|
zip([A | ACdr], [B | BCdr], [A-B | ABCdr]) :- zip(ACdr, BCdr, ABCdr).
|
||||||
|
Loading…
Reference in New Issue
Block a user