Added error handling for missing data files
This commit is contained in:
parent
3f4a64b6e0
commit
824ec0e29b
|
@ -6,7 +6,7 @@ This is a single command line application for all puzzles written in [FreePascal
|
||||||
|
|
||||||
## Puzzle Input
|
## Puzzle Input
|
||||||
|
|
||||||
This project does not contain the puzzle or example inputs as per the [copyright notice of Advent of Code](https://adventofcode.com/about).
|
This project does not contain the puzzle or example inputs as per the [copyright notice of Advent of Code](https://adventofcode.com/about). In order to run the compiled application, the puzzle inputs have to be downloaded from the [Advent of Code 2023](https://adventofcode.com/2023/) puzzle pages, and placed as text files into the `bin\data` directory. The application will output an error with details, if it cannot find an input file.
|
||||||
|
|
||||||
## Day 1: Trebuchet?!
|
## Day 1: Trebuchet?!
|
||||||
|
|
||||||
|
|
24
USolver.pas
24
USolver.pas
|
@ -69,6 +69,7 @@ type
|
||||||
procedure ProcessData(const ASolver: ISolver);
|
procedure ProcessData(const ASolver: ISolver);
|
||||||
procedure Run(const ASolver: ISolver);
|
procedure Run(const ASolver: ISolver);
|
||||||
procedure RunAndFree(const ASolver: ISolver);
|
procedure RunAndFree(const ASolver: ISolver);
|
||||||
|
function GetDataFileName(const ASolver: ISolver): string;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
@ -105,7 +106,7 @@ var
|
||||||
begin
|
begin
|
||||||
ASolver.Init;
|
ASolver.Init;
|
||||||
|
|
||||||
AssignFile(data, ConcatPaths([FRelativeDataPath, ASolver.DataFileName]));
|
AssignFile(data, GetDataFileName(ASolver));
|
||||||
try
|
try
|
||||||
reset(data);
|
reset(data);
|
||||||
while (not EOF(data)) do
|
while (not EOF(data)) do
|
||||||
|
@ -121,12 +122,22 @@ begin
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TSolverEngine.Run(const ASolver: ISolver);
|
procedure TSolverEngine.Run(const ASolver: ISolver);
|
||||||
|
var
|
||||||
|
fileName: string;
|
||||||
begin
|
begin
|
||||||
WriteLn;
|
WriteLn;
|
||||||
WriteLn('--- ', ASolver.PuzzleName, ' ---');
|
WriteLn('--- ', ASolver.PuzzleName, ' ---');
|
||||||
ProcessData(ASolver);
|
fileName := GetDataFileName(ASolver);
|
||||||
WriteLn('Part 1: ', ASolver.ResultPart1);
|
if FileExists(fileName) then
|
||||||
WriteLn('Part 2: ', ASolver.ResultPart2);
|
begin
|
||||||
|
ProcessData(ASolver);
|
||||||
|
WriteLn('Part 1: ', ASolver.ResultPart1);
|
||||||
|
WriteLn('Part 2: ', ASolver.ResultPart2);
|
||||||
|
end
|
||||||
|
else begin
|
||||||
|
WriteLn('Cannot find puzzle input file ''', ExpandFileName(fileName), '''.');
|
||||||
|
WriteLn('Please download the file content from https://adventofcode.com/2023/');
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TSolverEngine.RunAndFree(const ASolver: ISolver);
|
procedure TSolverEngine.RunAndFree(const ASolver: ISolver);
|
||||||
|
@ -135,5 +146,10 @@ begin
|
||||||
ASolver.Free;
|
ASolver.Free;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TSolverEngine.GetDataFileName(const ASolver: ISolver): string;
|
||||||
|
begin
|
||||||
|
Result := ConcatPaths([FRelativeDataPath, ASolver.DataFileName]);
|
||||||
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
|
@ -72,9 +72,15 @@ end;
|
||||||
{ TEngineBaseTest }
|
{ TEngineBaseTest }
|
||||||
|
|
||||||
procedure TEngineBaseTest.Setup;
|
procedure TEngineBaseTest.Setup;
|
||||||
|
var
|
||||||
|
fileName: string;
|
||||||
begin
|
begin
|
||||||
inherited Setup;
|
inherited Setup;
|
||||||
FEngine := TSolverEngine.Create(GetDataPath);
|
FEngine := TSolverEngine.Create(GetDataPath);
|
||||||
|
fileName := FEngine.GetDataFileName(FSolver);
|
||||||
|
AssertTrue('Cannot find puzzle input file ''' + ExpandFileName(fileName) + '''. '
|
||||||
|
+ 'Please download the file from https://adventofcode.com/2023/',
|
||||||
|
FileExists(fileName));
|
||||||
FEngine.ProcessData(FSolver);
|
FEngine.ProcessData(FSolver);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue