Added solution for "Day 6: Wait For It", part 1
This commit is contained in:
parent
94bc00ab1c
commit
93f6ceb1db
|
@ -53,6 +53,10 @@
|
|||
<Filename Value="solvers\UGiveSeedFertilizer.pas"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
</Unit>
|
||||
<Unit>
|
||||
<Filename Value="solvers\UWaitForIt.pas"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
</Unit>
|
||||
</Units>
|
||||
</ProjectOptions>
|
||||
<CompilerOptions>
|
||||
|
|
|
@ -25,7 +25,7 @@ uses
|
|||
{$ENDIF}
|
||||
Classes, SysUtils, CustApp,
|
||||
USolver,
|
||||
UTrebuchet, UCubeConundrum, UGearRatios, UScratchcards, UGiveSeedFertilizer;
|
||||
UTrebuchet, UCubeConundrum, UGearRatios, UScratchcards, UGiveSeedFertilizer, UWaitForIt;
|
||||
|
||||
type
|
||||
|
||||
|
@ -55,6 +55,7 @@ begin
|
|||
engine.RunAndFree(TGearRatios.Create);
|
||||
engine.RunAndFree(TScratchcards.Create);
|
||||
engine.RunAndFree(TGiveSeedFertilizer.Create);
|
||||
engine.RunAndFree(TWaitForIt.Create);
|
||||
engine.Free;
|
||||
end;
|
||||
|
||||
|
|
|
@ -0,0 +1,107 @@
|
|||
{
|
||||
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 UWaitForIt;
|
||||
|
||||
{$mode ObjFPC}{$H+}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, fgl, Math, USolver;
|
||||
|
||||
type
|
||||
|
||||
{ TWaitForIt }
|
||||
|
||||
TWaitForIt = class(TSolver)
|
||||
FTimes, FDistances: specialize TFPGList<Cardinal>;
|
||||
public
|
||||
constructor Create;
|
||||
destructor Destroy; override;
|
||||
procedure ProcessDataLine(const ALine: string); override;
|
||||
procedure Finish; override;
|
||||
function GetDataFileName: string; override;
|
||||
function GetPuzzleName: string; override;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
{ TWaitForIt }
|
||||
|
||||
constructor TWaitForIt.Create;
|
||||
begin
|
||||
FTimes := specialize TFPGList<Cardinal>.Create;
|
||||
FDistances := specialize TFPGList<Cardinal>.Create;
|
||||
end;
|
||||
|
||||
destructor TWaitForIt.Destroy;
|
||||
begin
|
||||
FTimes.Free;
|
||||
FDistances.Free;
|
||||
inherited Destroy;
|
||||
end;
|
||||
|
||||
procedure TWaitForIt.ProcessDataLine(const ALine: string);
|
||||
var
|
||||
split: TStringArray;
|
||||
list: specialize TFPGList<Cardinal>;
|
||||
i: Integer;
|
||||
begin
|
||||
split := ALine.Split([' ', ':']);
|
||||
if split[0] = 'Time' then
|
||||
list := FTimes
|
||||
else if split[0] = 'Distance' then
|
||||
list := FDistances
|
||||
else
|
||||
Exit;
|
||||
|
||||
for i := 1 to Length(split) - 1 do
|
||||
if split[i] <> '' then
|
||||
begin
|
||||
list.Add(StrToDWord(split[i]));
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TWaitForIt.Finish;
|
||||
var
|
||||
i, x1, x2: Integer;
|
||||
p, s: Double;
|
||||
begin
|
||||
FPart1 := 1;
|
||||
for i := 0 to FTimes.Count - 1 do
|
||||
begin
|
||||
p := FTimes[i] / 2;
|
||||
s := Sqrt(p * p - FDistances[i]);
|
||||
x1 := Math.Floor(p - s + 1);
|
||||
x2 := Math.Ceil(p + s - 1);
|
||||
FPart1 := FPart1 * (x2 - x1 + 1);
|
||||
end;
|
||||
end;
|
||||
|
||||
function TWaitForIt.GetDataFileName: string;
|
||||
begin
|
||||
Result := 'wait_for_it.txt';
|
||||
end;
|
||||
|
||||
function TWaitForIt.GetPuzzleName: string;
|
||||
begin
|
||||
Result := 'Day 6: Wait For It';
|
||||
end;
|
||||
|
||||
end.
|
||||
|
|
@ -64,6 +64,10 @@
|
|||
<Filename Value="UCubeConundrumTestCases.pas"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
</Unit>
|
||||
<Unit>
|
||||
<Filename Value="UWaitForItTestCases.pas"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
</Unit>
|
||||
</Units>
|
||||
</ProjectOptions>
|
||||
<CompilerOptions>
|
||||
|
|
|
@ -4,7 +4,7 @@ program AdventOfCodeFPCUnit;
|
|||
|
||||
uses
|
||||
Interfaces, Forms, GuiTestRunner, USolver, UBaseTestCases, UTrebuchetTestCases, UCubeConundrumTestCases,
|
||||
UGearRatiosTestCases, UScratchcardsTestCases, UGiveSeedFertilizerTestCases;
|
||||
UGearRatiosTestCases, UScratchcardsTestCases, UGiveSeedFertilizerTestCases, UWaitForItTestCases;
|
||||
|
||||
{$R *.res}
|
||||
|
||||
|
|
|
@ -0,0 +1,89 @@
|
|||
{
|
||||
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 UWaitForItTestCases;
|
||||
|
||||
{$mode ObjFPC}{$H+}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, fpcunit, testregistry, USolver, UBaseTestCases, UWaitForIt;
|
||||
|
||||
type
|
||||
|
||||
{ TWaitForItFullDataTestCase }
|
||||
|
||||
TWaitForItFullDataTestCase = class(TEngineBaseTest)
|
||||
protected
|
||||
function CreateSolver: ISolver; override;
|
||||
published
|
||||
procedure TestPart1;
|
||||
procedure TestPart2;
|
||||
end;
|
||||
|
||||
{ TWaitForItExampleTestCase }
|
||||
|
||||
TWaitForItExampleTestCase = class(TExampleEngineBaseTest)
|
||||
protected
|
||||
function CreateSolver: ISolver; override;
|
||||
published
|
||||
procedure TestPart1;
|
||||
procedure TestPart2;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
{ TWaitForItFullDataTestCase }
|
||||
|
||||
function TWaitForItFullDataTestCase.CreateSolver: ISolver;
|
||||
begin
|
||||
Result := TWaitForIt.Create;
|
||||
end;
|
||||
|
||||
procedure TWaitForItFullDataTestCase.TestPart1;
|
||||
begin
|
||||
AssertEquals(4811940, FSolver.GetResultPart1);
|
||||
end;
|
||||
|
||||
procedure TWaitForItFullDataTestCase.TestPart2;
|
||||
begin
|
||||
AssertEquals(-1, FSolver.GetResultPart2);
|
||||
end;
|
||||
|
||||
{ TWaitForItExampleTestCase }
|
||||
|
||||
function TWaitForItExampleTestCase.CreateSolver: ISolver;
|
||||
begin
|
||||
Result := TWaitForIt.Create;
|
||||
end;
|
||||
|
||||
procedure TWaitForItExampleTestCase.TestPart1;
|
||||
begin
|
||||
AssertEquals(288, FSolver.GetResultPart1);
|
||||
end;
|
||||
|
||||
procedure TWaitForItExampleTestCase.TestPart2;
|
||||
begin
|
||||
AssertEquals(-1, FSolver.GetResultPart2);
|
||||
end;
|
||||
|
||||
initialization
|
||||
|
||||
RegisterTest(TWaitForItFullDataTestCase);
|
||||
RegisterTest(TWaitForItExampleTestCase);
|
||||
end.
|
Loading…
Reference in New Issue