Added solution for "Day 4: Scratchcards", part 1
This commit is contained in:
105
solvers/UScratchcards.pas
Normal file
105
solvers/UScratchcards.pas
Normal file
@@ -0,0 +1,105 @@
|
||||
{
|
||||
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 UScratchcards;
|
||||
|
||||
{$mode ObjFPC}{$H+}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, USolver;
|
||||
|
||||
type
|
||||
|
||||
{ TScratchcards }
|
||||
|
||||
TScratchcards = class(TSolver)
|
||||
private
|
||||
function GetNumber(const AString: string; const AIndex: Integer): Integer;
|
||||
public
|
||||
procedure ProcessDataLine(const ALine: string); override;
|
||||
procedure Finish; override;
|
||||
function GetDataFileName: string; override;
|
||||
function GetPuzzleName: string; override;
|
||||
end;
|
||||
|
||||
|
||||
implementation
|
||||
|
||||
{ TScratchcards }
|
||||
|
||||
function TScratchcards.GetNumber(const AString: string; const AIndex: Integer): Integer;
|
||||
begin
|
||||
Result := StrToInt(Trim(Copy(AString, AIndex * 3 + 1, 3)))
|
||||
end;
|
||||
|
||||
procedure TScratchcards.ProcessDataLine(const ALine: string);
|
||||
var
|
||||
cardSplit: TStringArray;
|
||||
wins: array of Integer;
|
||||
count, i, have, win, cardPoints: Integer;
|
||||
begin
|
||||
cardSplit := ALine.Split([':', '|']);
|
||||
|
||||
// Determines winning numbers.
|
||||
count := cardSplit[1].Length div 3;
|
||||
SetLength(wins, count);
|
||||
for i := 0 to count - 1 do
|
||||
begin
|
||||
wins[i] := GetNumber(cardSplit[1], i);
|
||||
end;
|
||||
|
||||
// Checks have numbers against winning numbers.
|
||||
cardPoints := 0;
|
||||
count := cardSplit[2].Length div 3;
|
||||
for i := 0 to count - 1 do
|
||||
begin
|
||||
have := GetNumber(cardSplit[2], i);
|
||||
for win in wins do
|
||||
begin
|
||||
if win = have then
|
||||
begin
|
||||
if cardPoints = 0 then
|
||||
cardPoints := 1
|
||||
else
|
||||
Inc(cardPoints, cardPoints);
|
||||
Break;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
Inc(FPart1, cardPoints);
|
||||
end;
|
||||
|
||||
procedure TScratchcards.Finish;
|
||||
begin
|
||||
|
||||
end;
|
||||
|
||||
function TScratchcards.GetDataFileName: string;
|
||||
begin
|
||||
Result := 'scratchcards.txt';
|
||||
end;
|
||||
|
||||
function TScratchcards.GetPuzzleName: string;
|
||||
begin
|
||||
Result := 'Day 4: Scratchcards';
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
Reference in New Issue
Block a user