shorten even more

This commit is contained in:
Duy Truong 2023-12-07 15:04:57 -08:00
parent 22548a4339
commit d5ee7282e9

View File

@ -1,18 +1,13 @@
answer(Answer) :-
input(Input),
convert_input(Input, Hands, Bets),
input(Input), convert_input(Input, Hands, Bets),
convlist(score, Hands, Scores),
zip(Scores, Bets, ScoresBets),
sort(ScoresBets, SortedScoreBetsInv),
reverse(SortedScoreBetsInv, SortedScoreBets),
payout(SortedScoreBets, _, Answer).
% payout([Hand-Bet], _, Payout) computes total payout from [Hand-Bet] map.
payout([], 0, 0).
payout([_-Bet | Cdr], Rank, Payout) :-
payout(Cdr, LastRank, LastPayout),
Rank is LastRank + 1,
Payout is LastPayout + Rank*Bet.
sort(ScoresBets, SortedScoreBets),
length(Input, NHands), length(VarList, NHands),
scanl([_, Prev, Curr]>>(Curr is Prev + 1), VarList, 0, [_ | Ranks]),
zip(Ranks, SortedScoreBets, RankScoreBets),
foldl([Rank-(_-Bet), Prev, Sum]>>(Sum is Prev + Rank*Bet),
RankScoreBets, 0, Answer).
% score(Hand, Score) gets the score for one hand, that can be directly compared.
score([A, B, C, D, E], Score) :-
@ -47,8 +42,7 @@ card(84, 10). card(81, 12). card(75, 13). card(65, 14). % TQKA
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).
zip(List1, List2, Map) :- maplist([A, B, A-B]>>(true), List1, List2, Map).
% convert_input(InputList, Cards, Bets)
convert_input([], [], []).