diff --git a/AdventOfCode.lpi b/AdventOfCode.lpi index 2f2263e..327f6ee 100644 --- a/AdventOfCode.lpi +++ b/AdventOfCode.lpi @@ -53,6 +53,10 @@ + + + + diff --git a/AdventOfCode.lpr b/AdventOfCode.lpr index 50b4adf..faab668 100644 --- a/AdventOfCode.lpr +++ b/AdventOfCode.lpr @@ -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; diff --git a/solvers/UWaitForIt.pas b/solvers/UWaitForIt.pas new file mode 100644 index 0000000..d04a681 --- /dev/null +++ b/solvers/UWaitForIt.pas @@ -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 . +} + +unit UWaitForIt; + +{$mode ObjFPC}{$H+} + +interface + +uses + Classes, SysUtils, fgl, Math, USolver; + +type + + { TWaitForIt } + + TWaitForIt = class(TSolver) + FTimes, FDistances: specialize TFPGList; + 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.Create; + FDistances := specialize TFPGList.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; + 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. + diff --git a/tests/AdventOfCodeFPCUnit.lpi b/tests/AdventOfCodeFPCUnit.lpi index 4a9673f..f8c7705 100644 --- a/tests/AdventOfCodeFPCUnit.lpi +++ b/tests/AdventOfCodeFPCUnit.lpi @@ -64,6 +64,10 @@ + + + + diff --git a/tests/AdventOfCodeFPCUnit.lpr b/tests/AdventOfCodeFPCUnit.lpr index e21dae5..3df24ee 100644 --- a/tests/AdventOfCodeFPCUnit.lpr +++ b/tests/AdventOfCodeFPCUnit.lpr @@ -4,7 +4,7 @@ program AdventOfCodeFPCUnit; uses Interfaces, Forms, GuiTestRunner, USolver, UBaseTestCases, UTrebuchetTestCases, UCubeConundrumTestCases, - UGearRatiosTestCases, UScratchcardsTestCases, UGiveSeedFertilizerTestCases; + UGearRatiosTestCases, UScratchcardsTestCases, UGiveSeedFertilizerTestCases, UWaitForItTestCases; {$R *.res} diff --git a/tests/UWaitForItTestCases.pas b/tests/UWaitForItTestCases.pas new file mode 100644 index 0000000..f1242d8 --- /dev/null +++ b/tests/UWaitForItTestCases.pas @@ -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 . +} + +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.