aoc23/07/part1.pl
2023-12-07 01:50:17 -08:00

61 lines
2.1 KiB
Prolog

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([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.
% score(Hand, Score) gets the score for one hand, that can be directly compared.
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) get the type rank for one hand, ordered.
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(InputList, Cards, Bets)
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(Ascii, Value) converts between card ascii value and internal values
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 2 lists into a map
zip([], [], []).
zip([A | ACdr], [B | BCdr], [A-B | ABCdr]) :- zip(ACdr, BCdr, ABCdr).