Updated "Cube Conundrum" to use the solver-engine framework
This commit is contained in:
parent
c9e9eca35a
commit
2e13199796
|
@ -50,8 +50,8 @@ var
|
||||||
begin
|
begin
|
||||||
WriteLn('### Advent of Code 2023 ###');
|
WriteLn('### Advent of Code 2023 ###');
|
||||||
TTrebuchet.Run;
|
TTrebuchet.Run;
|
||||||
TCubeConundrum.Run;
|
|
||||||
engine := TSolverEngine.Create('data');
|
engine := TSolverEngine.Create('data');
|
||||||
|
engine.RunAndFree(TCubeConundrum.Create);
|
||||||
engine.RunAndFree(TGearRatios.Create);
|
engine.RunAndFree(TGearRatios.Create);
|
||||||
engine.RunAndFree(TScratchcards.Create);
|
engine.RunAndFree(TScratchcards.Create);
|
||||||
engine.RunAndFree(TGiveSeedFertilizer.Create);
|
engine.RunAndFree(TGiveSeedFertilizer.Create);
|
||||||
|
|
|
@ -22,7 +22,7 @@ unit UCubeConundrum;
|
||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
Classes, SysUtils;
|
Classes, SysUtils, USolver;
|
||||||
|
|
||||||
const
|
const
|
||||||
CMaxRed = 12;
|
CMaxRed = 12;
|
||||||
|
@ -33,35 +33,22 @@ type
|
||||||
|
|
||||||
{ TCubeConundrum }
|
{ TCubeConundrum }
|
||||||
|
|
||||||
TCubeConundrum = class(TObject)
|
TCubeConundrum = class(TSolver)
|
||||||
private
|
private
|
||||||
FValue1, FValue2, FGameMaxRed, FGameMaxGreen, FGameMaxBlue: Integer;
|
FGameMaxRed, FGameMaxGreen, FGameMaxBlue: Integer;
|
||||||
procedure ProcessDataLine(const ALine: string);
|
|
||||||
procedure ProcessGame(const id: Integer; const draws: TStringArray);
|
procedure ProcessGame(const id: Integer; const draws: TStringArray);
|
||||||
function ProcessDraw(const cubes: TStringArray): Boolean;
|
function ProcessDraw(const cubes: TStringArray): Boolean;
|
||||||
public
|
public
|
||||||
procedure Solve;
|
procedure ProcessDataLine(const ALine: string); override;
|
||||||
property Part1: Integer read FValue1;
|
procedure Finish; override;
|
||||||
property Part2: Integer read FValue2;
|
function GetDataFileName: string; override;
|
||||||
class procedure Run; static;
|
function GetPuzzleName: string; override;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
{ TCubeConundrum }
|
{ 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);
|
procedure TCubeConundrum.ProcessGame(const id: Integer; const draws: TStringArray);
|
||||||
var
|
var
|
||||||
draw: string;
|
draw: string;
|
||||||
|
@ -84,10 +71,10 @@ begin
|
||||||
if isWithinMaxima then
|
if isWithinMaxima then
|
||||||
begin
|
begin
|
||||||
// All draws fulfill maxima.
|
// All draws fulfill maxima.
|
||||||
Inc(FValue1, id);
|
Inc(FPart1, id);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
Inc(FValue2, FGameMaxRed * FGameMaxGreen * FGameMaxBlue);
|
Inc(FPart2, FGameMaxRed * FGameMaxGreen * FGameMaxBlue);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TCubeConundrum.ProcessDraw(const cubes: TStringArray): Boolean;
|
function TCubeConundrum.ProcessDraw(const cubes: TStringArray): Boolean;
|
||||||
|
@ -125,38 +112,31 @@ begin
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TCubeConundrum.Solve;
|
procedure TCubeConundrum.ProcessDataLine(const ALine: string);
|
||||||
var
|
var
|
||||||
data: TextFile;
|
gameSplit, subSplit: TStringArray;
|
||||||
s: string;
|
id: Integer;
|
||||||
begin
|
begin
|
||||||
FValue1 := 0;
|
gameSplit := ALine.Split(':');
|
||||||
FValue2 := 0;
|
subSplit := gameSplit[0].Split(' ');
|
||||||
|
id := StrToInt(subSplit[1]);
|
||||||
AssignFile(data, ConcatPaths(['data', 'cube_conundrum.txt']));
|
subSplit := gameSplit[1].Split(';');
|
||||||
try
|
ProcessGame(id, subSplit);
|
||||||
reset(data);
|
|
||||||
while (not EOF(data)) do
|
|
||||||
begin
|
|
||||||
readln(data, s);
|
|
||||||
ProcessDataLine(s);
|
|
||||||
end;
|
|
||||||
finally
|
|
||||||
CloseFile(data)
|
|
||||||
end;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class procedure TCubeConundrum.Run;
|
procedure TCubeConundrum.Finish;
|
||||||
var
|
|
||||||
cubeConundrum: TCubeConundrum;
|
|
||||||
begin
|
begin
|
||||||
WriteLn;
|
|
||||||
WriteLn('--- Day 2: Cube Conundrum ---');
|
end;
|
||||||
cubeConundrum := TCubeConundrum.Create;
|
|
||||||
cubeConundrum.Solve;
|
function TCubeConundrum.GetDataFileName: string;
|
||||||
WriteLn('Part 1: ', cubeConundrum.Part1);
|
begin
|
||||||
WriteLn('Part 2: ', cubeConundrum.Part2);
|
Result := 'cube_conundrum.txt';
|
||||||
cubeConundrum.Free;
|
end;
|
||||||
|
|
||||||
|
function TCubeConundrum.GetPuzzleName: string;
|
||||||
|
begin
|
||||||
|
Result := 'Day 2: Cube Conundrum';
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
|
Loading…
Reference in New Issue