shorten even more
This commit is contained in:
parent
22548a4339
commit
d5ee7282e9
22
07/part2.pl
22
07/part2.pl
@ -1,18 +1,13 @@
|
|||||||
answer(Answer) :-
|
answer(Answer) :-
|
||||||
input(Input),
|
input(Input), convert_input(Input, Hands, Bets),
|
||||||
convert_input(Input, Hands, Bets),
|
|
||||||
convlist(score, Hands, Scores),
|
convlist(score, Hands, Scores),
|
||||||
zip(Scores, Bets, ScoresBets),
|
zip(Scores, Bets, ScoresBets),
|
||||||
sort(ScoresBets, SortedScoreBetsInv),
|
sort(ScoresBets, SortedScoreBets),
|
||||||
reverse(SortedScoreBetsInv, SortedScoreBets),
|
length(Input, NHands), length(VarList, NHands),
|
||||||
payout(SortedScoreBets, _, Answer).
|
scanl([_, Prev, Curr]>>(Curr is Prev + 1), VarList, 0, [_ | Ranks]),
|
||||||
|
zip(Ranks, SortedScoreBets, RankScoreBets),
|
||||||
% payout([Hand-Bet], _, Payout) computes total payout from [Hand-Bet] map.
|
foldl([Rank-(_-Bet), Prev, Sum]>>(Sum is Prev + Rank*Bet),
|
||||||
payout([], 0, 0).
|
RankScoreBets, 0, Answer).
|
||||||
payout([_-Bet | Cdr], Rank, Payout) :-
|
|
||||||
payout(Cdr, LastRank, LastPayout),
|
|
||||||
Rank is LastRank + 1,
|
|
||||||
Payout is LastPayout + Rank*Bet.
|
|
||||||
|
|
||||||
% 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) :-
|
||||||
@ -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.
|
card(Char, Card) :- Char >= 48, Char =< 57, Card is Char - 48.
|
||||||
|
|
||||||
% zip 2 lists into a map
|
% zip 2 lists into a map
|
||||||
zip([], [], []).
|
zip(List1, List2, Map) :- maplist([A, B, A-B]>>(true), List1, List2, Map).
|
||||||
zip([A | ACdr], [B | BCdr], [A-B | ABCdr]) :- zip(ACdr, BCdr, ABCdr).
|
|
||||||
|
|
||||||
% convert_input(InputList, Cards, Bets)
|
% convert_input(InputList, Cards, Bets)
|
||||||
convert_input([], [], []).
|
convert_input([], [], []).
|
||||||
|
Loading…
Reference in New Issue
Block a user