Updated "Cube Conundrum" to use the solver-engine framework

This commit is contained in:
Stefan Müller 2023-12-05 15:45:57 +01:00 committed by Stefan Müller
parent c9e9eca35a
commit 2e13199796
2 changed files with 30 additions and 50 deletions

View File

@ -50,8 +50,8 @@ var
begin
WriteLn('### Advent of Code 2023 ###');
TTrebuchet.Run;
TCubeConundrum.Run;
engine := TSolverEngine.Create('data');
engine.RunAndFree(TCubeConundrum.Create);
engine.RunAndFree(TGearRatios.Create);
engine.RunAndFree(TScratchcards.Create);
engine.RunAndFree(TGiveSeedFertilizer.Create);

View File

@ -22,7 +22,7 @@ unit UCubeConundrum;
interface
uses
Classes, SysUtils;
Classes, SysUtils, USolver;
const
CMaxRed = 12;
@ -33,35 +33,22 @@ type
{ TCubeConundrum }
TCubeConundrum = class(TObject)
TCubeConundrum = class(TSolver)
private
FValue1, FValue2, FGameMaxRed, FGameMaxGreen, FGameMaxBlue: Integer;
procedure ProcessDataLine(const ALine: string);
FGameMaxRed, FGameMaxGreen, FGameMaxBlue: Integer;
procedure ProcessGame(const id: Integer; const draws: TStringArray);
function ProcessDraw(const cubes: TStringArray): Boolean;
public
procedure Solve;
property Part1: Integer read FValue1;
property Part2: Integer read FValue2;
class procedure Run; static;
procedure ProcessDataLine(const ALine: string); override;
procedure Finish; override;
function GetDataFileName: string; override;
function GetPuzzleName: string; override;
end;
implementation
{ TCubeConundrum }
procedure TCubeConundrum.ProcessDataLine(const ALine: string);
var
gameSplit, subSplit: TStringArray;
id: Integer;
begin
gameSplit := ALine.Split(':');
subSplit := gameSplit[0].Split(' ');
id := StrToInt(subSplit[1]);
subSplit := gameSplit[1].Split(';');
ProcessGame(id, subSplit);
end;
procedure TCubeConundrum.ProcessGame(const id: Integer; const draws: TStringArray);
var
draw: string;
@ -84,10 +71,10 @@ begin
if isWithinMaxima then
begin
// All draws fulfill maxima.
Inc(FValue1, id);
Inc(FPart1, id);
end;
Inc(FValue2, FGameMaxRed * FGameMaxGreen * FGameMaxBlue);
Inc(FPart2, FGameMaxRed * FGameMaxGreen * FGameMaxBlue);
end;
function TCubeConundrum.ProcessDraw(const cubes: TStringArray): Boolean;
@ -125,38 +112,31 @@ begin
end;
end;
procedure TCubeConundrum.Solve;
procedure TCubeConundrum.ProcessDataLine(const ALine: string);
var
data: TextFile;
s: string;
gameSplit, subSplit: TStringArray;
id: Integer;
begin
FValue1 := 0;
FValue2 := 0;
AssignFile(data, ConcatPaths(['data', 'cube_conundrum.txt']));
try
reset(data);
while (not EOF(data)) do
begin
readln(data, s);
ProcessDataLine(s);
end;
finally
CloseFile(data)
end;
gameSplit := ALine.Split(':');
subSplit := gameSplit[0].Split(' ');
id := StrToInt(subSplit[1]);
subSplit := gameSplit[1].Split(';');
ProcessGame(id, subSplit);
end;
class procedure TCubeConundrum.Run;
var
cubeConundrum: TCubeConundrum;
procedure TCubeConundrum.Finish;
begin
WriteLn;
WriteLn('--- Day 2: Cube Conundrum ---');
cubeConundrum := TCubeConundrum.Create;
cubeConundrum.Solve;
WriteLn('Part 1: ', cubeConundrum.Part1);
WriteLn('Part 2: ', cubeConundrum.Part2);
cubeConundrum.Free;
end;
function TCubeConundrum.GetDataFileName: string;
begin
Result := 'cube_conundrum.txt';
end;
function TCubeConundrum.GetPuzzleName: string;
begin
Result := 'Day 2: Cube Conundrum';
end;
end.