214 lines
4.7 KiB
Plaintext
214 lines
4.7 KiB
Plaintext
{
|
|
Solutions to the Advent Of Code.
|
|
Copyright (C) 2023 Stefan Müller
|
|
|
|
This program is free software: you can redistribute it and/or modify it under
|
|
the terms of the GNU General Public License as published by the Free Software
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
version.
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
this program. If not, see <http://www.gnu.org/licenses/>.
|
|
}
|
|
|
|
unit UCamelCards;
|
|
|
|
{$mode ObjFPC}{$H+}
|
|
|
|
interface
|
|
|
|
uses
|
|
Classes, SysUtils, Generics.Collections, Generics.Defaults, USolver;
|
|
|
|
type
|
|
TCardHandType = (htHighCard, htOnePair, htTwoPair, htThreeOfAKind, htFullHouse, htFourOfAKind, htFiveOfAKind);
|
|
|
|
TCardCounter = record
|
|
Card: Char;
|
|
Count: Integer;
|
|
end;
|
|
|
|
TCardCounters = specialize TList<TCardCounter>;
|
|
|
|
{ TCardHand }
|
|
|
|
TCardHand = class
|
|
private
|
|
FHand: string;
|
|
FType: TCardHandType;
|
|
FBid: Cardinal;
|
|
function GetCardValue(const ACard: Char): Cardinal;
|
|
public
|
|
constructor Create(const ALine: string);
|
|
function CompareTo(constref AOther: TCardHand): Integer;
|
|
property Bid: Cardinal read FBid;
|
|
end;
|
|
|
|
TCardHands = specialize TObjectList<TCardHand>;
|
|
|
|
{ TCardHandComparer }
|
|
|
|
TCardHandComparer = class(TInterfacedObject, specialize IComparer<TCardHand>)
|
|
public
|
|
function Compare(constref AHand1, AHand2: TCardHand): Integer;
|
|
end;
|
|
|
|
{ TCamelCards }
|
|
|
|
TCamelCards = class(TSolver)
|
|
private
|
|
FHands: TCardHands;
|
|
public
|
|
constructor Create;
|
|
destructor Destroy; override;
|
|
procedure ProcessDataLine(const ALine: string); override;
|
|
procedure Finish; override;
|
|
function GetDataFileName: string; override;
|
|
function GetPuzzleName: string; override;
|
|
end;
|
|
|
|
implementation
|
|
|
|
{ TCardHand }
|
|
|
|
function TCardHand.GetCardValue(const ACard: Char): Cardinal;
|
|
begin
|
|
if not TryStrToDWord(ACard, Result) then
|
|
case ACard of
|
|
'A': Result := 14;
|
|
'K': Result := 13;
|
|
'Q': Result := 12;
|
|
'J': Result := 11;
|
|
'T': Result := 10;
|
|
else Result := 0;
|
|
end;
|
|
end;
|
|
|
|
constructor TCardHand.Create(const ALine: string);
|
|
var
|
|
split: TStringArray;
|
|
counters: TCardCounters;
|
|
counter: TCardCounter;
|
|
i: Integer;
|
|
c: Char;
|
|
found: Boolean;
|
|
begin
|
|
split := ALine.Split(' ');
|
|
FHand := split[0];
|
|
FBid := StrToDWord(split[1]);
|
|
|
|
counters := TCardCounters.Create;
|
|
|
|
// Creates a counter for each different card value and counts cards per card value.
|
|
for c in FHand do
|
|
begin
|
|
found := False;
|
|
i := 0;
|
|
while not found and (i < counters.Count) do
|
|
begin
|
|
if counters[i].Card = c then
|
|
begin
|
|
found := True;
|
|
counter := counters[i];
|
|
Inc(counter.Count);
|
|
counters[i] := counter;
|
|
end;
|
|
Inc(i);
|
|
end;
|
|
|
|
if not found then
|
|
begin
|
|
counter.Card := c;
|
|
counter.Count := 1;
|
|
counters.Add(counter);
|
|
end;
|
|
end;
|
|
|
|
// Determines the hand type from the counters.
|
|
case counters.Count of
|
|
1: FType := htFiveOfAKind;
|
|
2:
|
|
if (counters[0].Count = 4) or (counters[1].Count = 4) then
|
|
FType := htFourOfAKind
|
|
else
|
|
FType := htFullHouse;
|
|
3:
|
|
if (counters[0].Count = 3) or (counters[1].Count = 3) or (counters[2].Count = 3) then
|
|
FType := htThreeOfAKind
|
|
else
|
|
FType := htTwoPair;
|
|
4: FType := htOnePair;
|
|
5: FType := htHighCard;
|
|
end;
|
|
|
|
counters.Free;
|
|
end;
|
|
|
|
function TCardHand.CompareTo(constref AOther: TCardHand): Integer;
|
|
var
|
|
i: Integer;
|
|
begin
|
|
Result := Ord(FType) - Ord(AOther.FType);
|
|
i := 1;
|
|
while (Result = 0) and (i <= FHand.Length) do
|
|
begin
|
|
Result := GetCardValue(FHand[i]) - GetCardValue(AOther.FHand[i]);
|
|
Inc(i);
|
|
end;
|
|
end;
|
|
|
|
{ TCardHandComparer }
|
|
|
|
function TCardHandComparer.Compare(constref AHand1, AHand2: TCardHand): Integer;
|
|
begin
|
|
Result := AHand1.CompareTo(AHand2);
|
|
end;
|
|
|
|
{ TCamelCards }
|
|
|
|
constructor TCamelCards.Create;
|
|
begin
|
|
FHands := TCardHands.Create;
|
|
end;
|
|
|
|
destructor TCamelCards.Destroy;
|
|
begin
|
|
FHands.Free;
|
|
inherited Destroy;
|
|
end;
|
|
|
|
procedure TCamelCards.ProcessDataLine(const ALine: string);
|
|
begin
|
|
FHands.Add(TCardHand.Create(ALine));
|
|
end;
|
|
|
|
procedure TCamelCards.Finish;
|
|
var
|
|
comparer: TCardHandComparer;
|
|
i: Integer;
|
|
begin
|
|
comparer := TCardHandComparer.Create;
|
|
FHands.Sort(comparer);
|
|
comparer.Free;
|
|
|
|
for i := 0 to FHands.Count - 1 do
|
|
Inc(FPart1, FHands[i].Bid * (i + 1));
|
|
end;
|
|
|
|
function TCamelCards.GetDataFileName: string;
|
|
begin
|
|
Result := 'camel_cards.txt';
|
|
end;
|
|
|
|
function TCamelCards.GetPuzzleName: string;
|
|
begin
|
|
Result := 'Day 7: Camel Cards';
|
|
end;
|
|
|
|
end.
|
|
|