AdventOfCode2023/puzzles/utrebuchet.pas

99 lines
1.9 KiB
Plaintext
Raw Normal View History

2023-12-02 17:42:53 +01:00
{
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 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.