Merged solution for "Day 12: Hot Springs", part 2

This commit is contained in:
2024-11-19 23:54:48 +01:00
9 changed files with 1170 additions and 101 deletions

View File

@@ -152,6 +152,10 @@
<Filename Value="USnowverloadTestCases.pas"/>
<IsPartOfProject Value="True"/>
</Unit>
<Unit>
<Filename Value="UBinomialCoefficientsTestCases.pas"/>
<IsPartOfProject Value="True"/>
</Unit>
</Units>
</ProjectOptions>
<CompilerOptions>

View File

@@ -10,7 +10,7 @@ uses
UFloorWillBeLavaTestCases, UClumsyCrucibleTestCases, ULavaductLagoonTestCases, UAplentyTestCases,
UPulsePropagationTestCases, UStepCounterTestCases, USandSlabsTestCases, ULongWalkTestCases,
UNeverTellMeTheOddsTestCases, USnowverloadTestCases, UBigIntTestCases, UPolynomialTestCases,
UPolynomialRootsTestCases;
UPolynomialRootsTestCases, UBinomialCoefficientsTestCases;
{$R *.res}

View File

@@ -0,0 +1,138 @@
{
Solutions to the Advent Of Code.
Copyright (C) 2024 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 UBinomialCoefficientsTestCases;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, fpcunit, testregistry, UBinomialCoefficients;
type
{ TBinomialCoefficientsTestCase }
TBinomialCoefficientsTestCase = class(TTestCase)
private
FBinomialCoefficientCache: TBinomialCoefficientCache;
procedure RunRangeError;
procedure AssertEqualsCalculation(const AN, AK, AExpected: Cardinal);
procedure AssertEqualsCachedRowsCount(const AExpected: Cardinal);
protected
procedure SetUp; override;
procedure TearDown; override;
published
procedure TestZeroChooseZero;
procedure TestNChooseZero;
procedure TestNChooseN;
procedure TestNChooseK;
procedure TestCombined;
procedure TestFullRow;
procedure TestRangeError;
end;
implementation
{ TBinomialCoefficientsTestCase }
procedure TBinomialCoefficientsTestCase.RunRangeError;
begin
FBinomialCoefficientCache.Get(1, 5);
end;
procedure TBinomialCoefficientsTestCase.AssertEqualsCalculation(const AN, AK, AExpected: Cardinal);
begin
AssertEquals('Unexpected calculation result', AExpected, FBinomialCoefficientCache.Get(AN, AK));
end;
procedure TBinomialCoefficientsTestCase.AssertEqualsCachedRowsCount(const AExpected: Cardinal);
begin
AssertEquals('Unexpected cached rows count', AExpected, FBinomialCoefficientCache.GetCachedRowsCount);
end;
procedure TBinomialCoefficientsTestCase.SetUp;
begin
FBinomialCoefficientCache := TBinomialCoefficientCache.Create;
end;
procedure TBinomialCoefficientsTestCase.TearDown;
begin
FBinomialCoefficientCache.Free;
end;
procedure TBinomialCoefficientsTestCase.TestZeroChooseZero;
begin
AssertEqualsCalculation(0, 0, 1);
AssertEqualsCachedRowsCount(1);
end;
procedure TBinomialCoefficientsTestCase.TestNChooseZero;
begin
AssertEqualsCalculation(15, 0, 1);
AssertEqualsCachedRowsCount(16);
end;
procedure TBinomialCoefficientsTestCase.TestNChooseN;
begin
AssertEqualsCalculation(11, 11, 1);
AssertEqualsCachedRowsCount(12);
end;
procedure TBinomialCoefficientsTestCase.TestNChooseK;
begin
AssertEqualsCalculation(8, 3, 56);
AssertEqualsCachedRowsCount(9);
end;
procedure TBinomialCoefficientsTestCase.TestCombined;
begin
AssertEqualsCalculation(5, 1, 5);
AssertEqualsCachedRowsCount(6);
AssertEqualsCalculation(8, 4, 70);
AssertEqualsCachedRowsCount(9);
AssertEqualsCalculation(3, 1, 3);
AssertEqualsCachedRowsCount(9);
end;
procedure TBinomialCoefficientsTestCase.TestFullRow;
begin
AssertEqualsCalculation(5, 0, 1);
AssertEqualsCachedRowsCount(6);
AssertEqualsCalculation(5, 1, 5);
AssertEqualsCachedRowsCount(6);
AssertEqualsCalculation(5, 2, 10);
AssertEqualsCachedRowsCount(6);
AssertEqualsCalculation(5, 3, 10);
AssertEqualsCachedRowsCount(6);
AssertEqualsCalculation(5, 4, 5);
AssertEqualsCachedRowsCount(6);
AssertEqualsCalculation(5, 5, 1);
AssertEqualsCachedRowsCount(6);
end;
procedure TBinomialCoefficientsTestCase.TestRangeError;
begin
AssertException(ERangeError, @RunRangeError);
end;
initialization
RegisterTest('Helper', TBinomialCoefficientsTestCase);
end.

View File

@@ -33,6 +33,7 @@ type
function CreateSolver: ISolver; override;
published
procedure TestPart1;
procedure TestPart2;
end;
{ THotSpringsTestCase }
@@ -40,14 +41,20 @@ type
THotSpringsTestCase = class(TSolverTestCase)
protected
function CreateSolver: ISolver; override;
procedure TestSingleLine(const ALine: string; const AValue: Integer);
procedure TestSingleLine(const ALine: string);
published
procedure TestExampleLine1;
procedure TestExampleLine2;
procedure TestExampleLine3;
procedure TestExampleLine4;
procedure TestExampleLine5;
procedure TestExampleLine6;
procedure TestExampleLine1Part1;
procedure TestExampleLine2Part1;
procedure TestExampleLine3Part1;
procedure TestExampleLine4Part1;
procedure TestExampleLine5Part1;
procedure TestExampleLine6Part1;
procedure TestExampleLine1Part2;
procedure TestExampleLine2Part2;
procedure TestExampleLine3Part2;
procedure TestExampleLine4Part2;
procedure TestExampleLine5Part2;
procedure TestExampleLine6Part2;
end;
implementation
@@ -64,6 +71,11 @@ begin
AssertEquals(21, FSolver.GetResultPart1);
end;
procedure THotSpringsExampleTestCase.TestPart2;
begin
AssertEquals(525152, FSolver.GetResultPart2);
end;
{ THotSpringsTestCase }
function THotSpringsTestCase.CreateSolver: ISolver;
@@ -71,42 +83,83 @@ begin
Result := THotSprings.Create;
end;
procedure THotSpringsTestCase.TestSingleLine(const ALine: string; const AValue: Integer);
procedure THotSpringsTestCase.TestSingleLine(const ALine: string);
begin
FSolver.Init;
FSolver.ProcessDataLine(ALine);
FSolver.Finish;
AssertEquals(AValue, FSolver.GetResultPart1);
end;
procedure THotSpringsTestCase.TestExampleLine1;
procedure THotSpringsTestCase.TestExampleLine1Part1;
begin
TestSingleLine('???.### 1,1,3', 1);
TestSingleLine('???.### 1,1,3');
AssertEquals(1, FSolver.GetResultPart1);
end;
procedure THotSpringsTestCase.TestExampleLine2;
procedure THotSpringsTestCase.TestExampleLine2Part1;
begin
TestSingleLine('.??..??...?##. 1,1,3', 4);
TestSingleLine('.??..??...?##. 1,1,3');
AssertEquals(4, FSolver.GetResultPart1);
end;
procedure THotSpringsTestCase.TestExampleLine3;
procedure THotSpringsTestCase.TestExampleLine3Part1;
begin
TestSingleLine('?#?#?#?#?#?#?#? 1,3,1,6', 1);
TestSingleLine('?#?#?#?#?#?#?#? 1,3,1,6');
AssertEquals(1, FSolver.GetResultPart1);
end;
procedure THotSpringsTestCase.TestExampleLine4;
procedure THotSpringsTestCase.TestExampleLine4Part1;
begin
TestSingleLine('????.#...#... 4,1,1', 1);
TestSingleLine('????.#...#... 4,1,1');
AssertEquals(1, FSolver.GetResultPart1);
end;
procedure THotSpringsTestCase.TestExampleLine5;
procedure THotSpringsTestCase.TestExampleLine5Part1;
begin
TestSingleLine('????.######..#####. 1,6,5', 4);
TestSingleLine('????.######..#####. 1,6,5');
AssertEquals(4, FSolver.GetResultPart1);
end;
procedure THotSpringsTestCase.TestExampleLine6;
procedure THotSpringsTestCase.TestExampleLine6Part1;
begin
TestSingleLine('?###???????? 3,2,1', 10);
TestSingleLine('?###???????? 3,2,1');
AssertEquals(10, FSolver.GetResultPart1);
end;
procedure THotSpringsTestCase.TestExampleLine1Part2;
begin
TestSingleLine('???.### 1,1,3');
AssertEquals(1, FSolver.GetResultPart2);
end;
procedure THotSpringsTestCase.TestExampleLine2Part2;
begin
TestSingleLine('.??..??...?##. 1,1,3');
AssertEquals(16384, FSolver.GetResultPart2);
end;
procedure THotSpringsTestCase.TestExampleLine3Part2;
begin
TestSingleLine('?#?#?#?#?#?#?#? 1,3,1,6');
AssertEquals(1, FSolver.GetResultPart2);
end;
procedure THotSpringsTestCase.TestExampleLine4Part2;
begin
TestSingleLine('????.#...#... 4,1,1');
AssertEquals(16, FSolver.GetResultPart2);
end;
procedure THotSpringsTestCase.TestExampleLine5Part2;
begin
TestSingleLine('????.######..#####. 1,6,5');
AssertEquals(2500, FSolver.GetResultPart2);
end;
procedure THotSpringsTestCase.TestExampleLine6Part2;
begin
TestSingleLine('?###???????? 3,2,1');
AssertEquals(506250, FSolver.GetResultPart2);
end;
initialization