Added solution for "Day 4: Scratchcards", part 2
This commit is contained in:
parent
85d8cafedd
commit
cc868a6c0a
|
@ -22,7 +22,7 @@ unit UScratchcards;
|
||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
Classes, SysUtils, USolver;
|
Classes, SysUtils, fgl, USolver;
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
||||||
|
@ -30,8 +30,11 @@ type
|
||||||
|
|
||||||
TScratchcards = class(TSolver)
|
TScratchcards = class(TSolver)
|
||||||
private
|
private
|
||||||
|
FCardCopies: specialize TFPGList<Integer>;
|
||||||
function GetNumber(const AString: string; const AIndex: Integer): Integer;
|
function GetNumber(const AString: string; const AIndex: Integer): Integer;
|
||||||
public
|
public
|
||||||
|
constructor Create;
|
||||||
|
destructor Destroy; override;
|
||||||
procedure ProcessDataLine(const ALine: string); override;
|
procedure ProcessDataLine(const ALine: string); override;
|
||||||
procedure Finish; override;
|
procedure Finish; override;
|
||||||
function GetDataFileName: string; override;
|
function GetDataFileName: string; override;
|
||||||
|
@ -48,12 +51,33 @@ begin
|
||||||
Result := StrToInt(Trim(Copy(AString, AIndex * 3 + 1, 3)))
|
Result := StrToInt(Trim(Copy(AString, AIndex * 3 + 1, 3)))
|
||||||
end;
|
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);
|
procedure TScratchcards.ProcessDataLine(const ALine: string);
|
||||||
var
|
var
|
||||||
cardSplit: TStringArray;
|
cardSplit: TStringArray;
|
||||||
wins: array of Integer;
|
wins: array of Integer;
|
||||||
count, i, have, win, cardPoints: Integer;
|
count, i, have, win, cardPoints, cardMatches, cardCopies: Integer;
|
||||||
begin
|
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([':', '|']);
|
cardSplit := ALine.Split([':', '|']);
|
||||||
|
|
||||||
// Determines winning numbers.
|
// Determines winning numbers.
|
||||||
|
@ -66,6 +90,7 @@ begin
|
||||||
|
|
||||||
// Checks have numbers against winning numbers.
|
// Checks have numbers against winning numbers.
|
||||||
cardPoints := 0;
|
cardPoints := 0;
|
||||||
|
cardMatches := 0;
|
||||||
count := cardSplit[2].Length div 3;
|
count := cardSplit[2].Length div 3;
|
||||||
for i := 0 to count - 1 do
|
for i := 0 to count - 1 do
|
||||||
begin
|
begin
|
||||||
|
@ -78,12 +103,24 @@ begin
|
||||||
cardPoints := 1
|
cardPoints := 1
|
||||||
else
|
else
|
||||||
Inc(cardPoints, cardPoints);
|
Inc(cardPoints, cardPoints);
|
||||||
|
Inc(cardMatches);
|
||||||
Break;
|
Break;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
Inc(FPart1, cardPoints);
|
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;
|
end;
|
||||||
|
|
||||||
procedure TScratchcards.Finish;
|
procedure TScratchcards.Finish;
|
||||||
|
|
|
@ -78,7 +78,7 @@ end;
|
||||||
|
|
||||||
procedure TScratchcardsFullDataTestCase.TestPart2;
|
procedure TScratchcardsFullDataTestCase.TestPart2;
|
||||||
begin
|
begin
|
||||||
AssertEquals(-1, FSolver.GetResultPart2);
|
AssertEquals(5539496, FSolver.GetResultPart2);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
{ TScratchcardsExampleTestCase }
|
{ TScratchcardsExampleTestCase }
|
||||||
|
@ -103,7 +103,7 @@ end;
|
||||||
|
|
||||||
procedure TScratchcardsExampleTestCase.TestPart2;
|
procedure TScratchcardsExampleTestCase.TestPart2;
|
||||||
begin
|
begin
|
||||||
AssertEquals(-1, FSolver.GetResultPart2);
|
AssertEquals(30, FSolver.GetResultPart2);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
initialization
|
initialization
|
||||||
|
|
Loading…
Reference in New Issue