2023-12-07 13:40:06 -05:00
|
|
|
% 0 for the joker. the other clauses are just the hand shapes
|
|
|
|
% sorry about all the cuts
|
|
|
|
sorted_hand([0|Rest], H) :- sorted_hand(Rest, H0), promote(H0, H), !.
|
|
|
|
sorted_hand([X,X,X,X,X], five) :- !.
|
|
|
|
sorted_hand([X,X,X,X|_], four) :- !.
|
2023-12-07 13:00:36 -05:00
|
|
|
sorted_hand([X,X,X,Y,Y], full_house) :- !.
|
|
|
|
sorted_hand([X,X,Y,Y,Y], full_house) :- !.
|
2023-12-07 13:40:06 -05:00
|
|
|
sorted_hand([X,X,X|_], three) :- !.
|
|
|
|
sorted_hand([X,X|Rest], two_pairs) :- sorted_hand(Rest, pair), !.
|
|
|
|
sorted_hand([X,X|_], pair) :- !.
|
|
|
|
sorted_hand([_|Rest], H) :- sorted_hand(Rest, H), !.
|
|
|
|
sorted_hand([], junk).
|
|
|
|
|
|
|
|
promote(junk, pair).
|
|
|
|
promote(pair, three).
|
|
|
|
promote(two_pairs, full_house).
|
|
|
|
promote(three, four).
|
|
|
|
promote(four, five).
|
|
|
|
|
|
|
|
hand(Cards, H) :- msort(Cards, S), sorted_hand(S, H).
|
|
|
|
|
|
|
|
rank(junk, 0).
|
|
|
|
rank(pair, 1).
|
|
|
|
rank(two_pairs, 2).
|
|
|
|
rank(three, 3).
|
2023-12-07 13:00:36 -05:00
|
|
|
rank(full_house, 4).
|
2023-12-07 13:40:06 -05:00
|
|
|
rank(four, 5). % lol
|
|
|
|
rank(five, 6).
|
2023-12-07 13:00:36 -05:00
|
|
|
|
|
|
|
hand_rank(C, R) :- hand(C, H), rank(H, R).
|
|
|
|
|
2023-12-07 14:18:09 -05:00
|
|
|
compare_rank(X, C1, C2) :-
|
|
|
|
hand_rank(C1, R1), hand_rank(C2, R2), compare(X, R1, R2).
|
2023-12-07 13:00:36 -05:00
|
|
|
|
2023-12-07 13:40:06 -05:00
|
|
|
compare_hand(X, C1-_, C2-_) :-
|
|
|
|
compare_rank(X0, C1, C2),
|
|
|
|
(X0 = (=) -> compare(X, C1, C2) ; X = X0).
|
|
|
|
|
2023-12-07 13:00:36 -05:00
|
|
|
|
2023-12-07 13:40:06 -05:00
|
|
|
total(Xs, R) :- total(1, Xs, R).
|
2023-12-07 13:00:36 -05:00
|
|
|
|
|
|
|
total(_, [], 0).
|
2023-12-07 14:18:09 -05:00
|
|
|
total(I, [_-X|Xs], R) :- I1 is I + 1, total(I1, Xs, R0), R is R0 + (I * X).
|
2023-12-07 13:00:36 -05:00
|
|
|
|
|
|
|
|
2023-12-07 13:40:06 -05:00
|
|
|
:- use_module(library(dcg/basics)).
|
2023-12-07 13:00:36 -05:00
|
|
|
|
2023-12-07 14:18:09 -05:00
|
|
|
card(V) --> [X], {char_code(C, X), value(C, V)}.
|
|
|
|
cards(Cs) --> card(A), card(B), card(C), card(D), card(E), {Cs = [A,B,C,D,E]}.
|
|
|
|
bid(N) --> digits(Ds), {number_chars(N, Ds)}.
|
|
|
|
spaces --> " ", (spaces ; []).
|
|
|
|
line(Cs-B) --> cards(Cs), spaces, bid(B).
|
|
|
|
|
2023-12-07 13:00:36 -05:00
|
|
|
lines([]) --> "\n" ; [].
|
|
|
|
lines([L|Ls]) --> line(L), "\n", lines(Ls).
|
|
|
|
|
2023-12-07 13:40:06 -05:00
|
|
|
value('T', 10).
|
|
|
|
value('J', 11).
|
|
|
|
value('Q', 12).
|
|
|
|
value('K', 13).
|
|
|
|
value('A', 14).
|
2023-12-07 14:18:09 -05:00
|
|
|
value(C, N) :- number_chars(N, [C]).
|
2023-12-07 13:40:06 -05:00
|
|
|
|
2023-12-07 13:00:36 -05:00
|
|
|
|
|
|
|
part1(File) :-
|
|
|
|
phrase_from_file(lines(Ls), File),
|
|
|
|
predsort(compare_hand, Ls, Sorted),
|
|
|
|
total(Sorted, R),
|
|
|
|
writeln(R), !.
|
|
|
|
|
|
|
|
|
2023-12-07 14:18:09 -05:00
|
|
|
part1_to_part2(Xs-B, Ys-B) :- maplist(joker, Xs, Ys).
|
2023-12-07 13:40:06 -05:00
|
|
|
joker(11, 0).
|
|
|
|
joker(X, X) :- X \= 11.
|
|
|
|
|
|
|
|
part2(File) :-
|
|
|
|
phrase_from_file(lines(Ls0), File),
|
|
|
|
maplist(part1_to_part2, Ls0, Ls),
|
|
|
|
predsort(compare_hand, Ls, Sorted),
|
|
|
|
total(Sorted, R),
|
|
|
|
writeln(R), !.
|
|
|
|
|
2023-12-07 13:00:36 -05:00
|
|
|
% vim: set ft=prolog :
|