AdventOfCode2023/puzzles/utrebuchet.pas

82 lines
1.2 KiB
Plaintext

unit UTrebuchet;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils;
type
{ TTrebuchet }
TTrebuchet = class(TObject)
private
FValue: Integer;
procedure RunSolution;
procedure ProcessDataLine(const ALine: string);
public
class procedure Solve; static;
constructor Create;
end;
implementation
{ TTrebuchet }
procedure TTrebuchet.RunSolution;
var
data: TextFile;
s: string;
begin
AssignFile(data, ConcatPaths(['data', 'trebuchet_calibration_document.txt']));
try
reset(data);
while (not EOF(data)) do
begin
readln(data, s);
ProcessDataLine(s);
end;
finally
CloseFile(data)
end;
WriteLn(FValue);
end;
procedure TTrebuchet.ProcessDataLine(const ALine: string);
var
c: Char;
first, last: Integer;
begin
first := -1;
last := -1;
for c in ALine do
begin
if c in ['0'..'9'] then
begin
last := StrToInt(c);
if first < 0 then
first := last;
end;
end;
Inc(FValue, first * 10 + last);
end;
class procedure TTrebuchet.Solve;
var
trebuchet: TTrebuchet;
begin
WriteLn('--- Day 1: Trebuchet?! ---');
trebuchet := TTrebuchet.Create;
trebuchet.RunSolution;
end;
constructor TTrebuchet.Create;
begin
FValue := 0;
end;
end.