This commit is contained in:
Dory 2023-12-07 00:06:31 -08:00
parent a191975108
commit 01e85ce954
5 changed files with 2068 additions and 0 deletions

1002
07/input.pl Normal file

File diff suppressed because it is too large Load Diff

1000
07/input.txt Normal file

File diff suppressed because it is too large Load Diff

54
07/part1.pl Normal file
View File

@ -0,0 +1,54 @@
answer(Answer) :-
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([], 0, 0).
payout([_-Bet | Cdr], Rank, Payout) :-
payout(Cdr, LastRank, LastPayout),
Rank is LastRank + 1,
Payout is LastPayout + Rank*Bet.
score([A, B, C, D, E], Score) :-
type([A, B, C, D, E], Type),
Score is E + 15*D + (15**2)*C + (15**3)*B + (15**4)*A + (15**5)*Type.
type(Hand, Type) :- sort(0, @=<, Hand, Sorted), sorted_type(Sorted, Type).
sorted_type([X, X, X, X, X], 7) :- !.
sorted_type([X, X, X, X, _], 6) :- !.
sorted_type([_, X, X, X, X], 6) :- !.
sorted_type([Y, Y, X, X, X], 5) :- !.
sorted_type([X, X, X, Y, Y], 5) :- !.
sorted_type([X, X, X, _, _], 4) :- !.
sorted_type([_, X, X, X, _], 4) :- !.
sorted_type([_, _, X, X, X], 4) :- !.
sorted_type([X, X, Y, Y, _], 3) :- !.
sorted_type([X, X, _, Y, Y], 3) :- !.
sorted_type([_, X, X, Y, Y], 3) :- !.
sorted_type([X, X, _, _, _], 2) :- !.
sorted_type([_, X, X, _, _], 2) :- !.
sorted_type([_, _, X, X, _], 2) :- !.
sorted_type([_, _, _, X, X], 2) :- !.
sorted_type([_, _, _, _, _], 1).
convert_input([], [], []).
convert_input([[Cards, Bet] | Cdr],
[[C1, C2, C3, C4, C5] | HandCdr],
[Bet | BetCdr]) :-
string_to_list(Cards, [A1, A2, A3, A4, A5]),
card(A1, C1), card(A2, C2), card(A3, C3), card(A4, C4), card(A5, C5),
convert_input(Cdr, HandCdr, BetCdr).
card(84, 10).
card(74, 11).
card(81, 12).
card(75, 13).
card(65, 14).
card(Char, Card) :- Char >= 48, Char =< 57, Card is Char - 48.
zip([], [], []).
zip([A | ACdr], [B | BCdr], [A-B | ABCdr]) :- zip(ACdr, BCdr, ABCdr).

7
07/test.pl Normal file
View File

@ -0,0 +1,7 @@
input([
["32T3K", 765],
["T55J5", 684],
["KK677", 28],
["KTJJT", 220],
["QQQJA", 483]
]).

5
07/test.txt Normal file
View File

@ -0,0 +1,5 @@
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483