Added solution for "Day 4: Scratchcards", part 2

This commit is contained in:
Stefan Müller 2023-12-04 21:59:39 +01:00 committed by Stefan Müller
parent 85d8cafedd
commit cc868a6c0a
2 changed files with 41 additions and 4 deletions

View File

@ -22,7 +22,7 @@ unit UScratchcards;
interface
uses
Classes, SysUtils, USolver;
Classes, SysUtils, fgl, USolver;
type
@ -30,8 +30,11 @@ type
TScratchcards = class(TSolver)
private
FCardCopies: specialize TFPGList<Integer>;
function GetNumber(const AString: string; const AIndex: Integer): Integer;
public
constructor Create;
destructor Destroy; override;
procedure ProcessDataLine(const ALine: string); override;
procedure Finish; override;
function GetDataFileName: string; override;
@ -48,12 +51,33 @@ begin
Result := StrToInt(Trim(Copy(AString, AIndex * 3 + 1, 3)))
end;
constructor TScratchcards.Create;
begin
FCardCopies := specialize TFPGList<Integer>.Create;
end;
destructor TScratchcards.Destroy;
begin
FCardCopies.Free;
inherited Destroy;
end;
procedure TScratchcards.ProcessDataLine(const ALine: string);
var
cardSplit: TStringArray;
wins: array of Integer;
count, i, have, win, cardPoints: Integer;
count, i, have, win, cardPoints, cardMatches, cardCopies: Integer;
begin
// Counts copies of this card.
if FCardCopies.Count > 0 then
begin
cardCopies := FCardCopies[0];
FCardCopies.Delete(0);
end
else
cardCopies := 1;
Inc(FPart2, cardCopies);
cardSplit := ALine.Split([':', '|']);
// Determines winning numbers.
@ -66,6 +90,7 @@ begin
// Checks have numbers against winning numbers.
cardPoints := 0;
cardMatches := 0;
count := cardSplit[2].Length div 3;
for i := 0 to count - 1 do
begin
@ -78,12 +103,24 @@ begin
cardPoints := 1
else
Inc(cardPoints, cardPoints);
Inc(cardMatches);
Break;
end;
end;
end;
Inc(FPart1, cardPoints);
// Adds copies of following cards and deletes the current card count.
if FCardCopies.Capacity < cardMatches then
FCardCopies.Capacity := cardMatches;
for i := 0 to cardMatches - 1 do
begin
if FCardCopies.Count <= i then
FCardCopies.Add(cardCopies + 1)
else
FCardCopies[i] := FCardCopies[i] + cardCopies;
end;
end;
procedure TScratchcards.Finish;

View File

@ -78,7 +78,7 @@ end;
procedure TScratchcardsFullDataTestCase.TestPart2;
begin
AssertEquals(-1, FSolver.GetResultPart2);
AssertEquals(5539496, FSolver.GetResultPart2);
end;
{ TScratchcardsExampleTestCase }
@ -103,7 +103,7 @@ end;
procedure TScratchcardsExampleTestCase.TestPart2;
begin
AssertEquals(-1, FSolver.GetResultPart2);
AssertEquals(30, FSolver.GetResultPart2);
end;
initialization