diff --git a/README.md b/README.md index 0b7d8a6..be34f2e 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,14 @@ The input data is such that there are only two pipes pointing to *S*, so it find For part 2, I tracked tiles that are "left" and "right" of the path the algorithm took, and implemented a little flood-fill algorithm that tries to fill the area in between the "left" tiles and the "right" tiles, while counting them. The "outside" group is the one where the flood-fill touches the edge of the map and is simply ignored. +## Day 11: Cosmic Expansion + + + +While parsing the input, we track coordinates of each galaxy, and for each row and column a *1* if it is empty and a *0* if not. At the end we sum for each pair of galaxies the values for each row and column between their coordinates *+1* to get the sum of their (Manhattan) distances. + +This approach was trivial to adapt for part 2, since all that was needed was another factor that had to be multiplied with the values tracked for the rows and columns before applying the *+1*. + ## License Copyright (C) 2023 Stefan Müller diff --git a/solvers/UCosmicExpansion.pas b/solvers/UCosmicExpansion.pas index 6f9ba96..522c74c 100644 --- a/solvers/UCosmicExpansion.pas +++ b/solvers/UCosmicExpansion.pas @@ -26,8 +26,8 @@ uses const CGalaxyChar = '#'; - CEmptySpaceFactor = 2; - CNonEmptySpaceFactor = 1; + CEmptySpaceFactor = 1; + CNonEmptySpaceFactor = 0; type @@ -45,6 +45,7 @@ type procedure Finish; override; function GetDataFileName: string; override; function GetPuzzleName: string; override; + function GetExpansionFactor: Integer; virtual; end; implementation @@ -107,9 +108,15 @@ begin for i := 0 to FGalaxies.Count - 1 do for j := i + 1 to FGalaxies.Count - 1 do begin for k := Min(FGalaxies[i].X, FGalaxies[j].X) to Max(FGalaxies[i].X, FGalaxies[j].X) - 1 do - Inc(FPart1, FColumnExpansion[k]); + begin + Inc(FPart1, 1 + FColumnExpansion[k]); + Inc(FPart2, 1 + FColumnExpansion[k] * GetExpansionFactor); + end; for k := Min(FGalaxies[i].Y, FGalaxies[j].Y) + 1 to Max(FGalaxies[i].Y, FGalaxies[j].Y) do - Inc(FPart1, FRowExpansion[k]); + begin + Inc(FPart1, 1 + FRowExpansion[k]); + Inc(FPart2, 1 + FRowExpansion[k] * GetExpansionFactor); + end; end; end; @@ -123,5 +130,10 @@ begin Result := 'Day 11: Cosmic Expansion'; end; +function TCosmicExpansion.GetExpansionFactor: Integer; +begin + Result := 999999; +end; + end. diff --git a/tests/UCosmicExpansionTestCases.pas b/tests/UCosmicExpansionTestCases.pas index 4093e91..5998fe6 100644 --- a/tests/UCosmicExpansionTestCases.pas +++ b/tests/UCosmicExpansionTestCases.pas @@ -33,6 +33,7 @@ type function CreateSolver: ISolver; override; published procedure TestPart1; + procedure TestPart2; end; { TCosmicExpansionExampleTestCase } @@ -44,6 +45,36 @@ type procedure TestPart1; end; + { TFactor10CosmicExpansion } + + TFactor10CosmicExpansion = class(TCosmicExpansion) + function GetExpansionFactor: Integer; override; + end; + + { TCosmicExpansionExampleFactor10TestCase } + + TCosmicExpansionExampleFactor10TestCase = class(TExampleEngineBaseTest) + protected + function CreateSolver: ISolver; override; + published + procedure TestPart2; + end; + + { TFactor100CosmicExpansion } + + TFactor100CosmicExpansion = class(TCosmicExpansion) + function GetExpansionFactor: Integer; override; + end; + + { TCosmicExpansionExampleFactor100TestCase } + + TCosmicExpansionExampleFactor100TestCase = class(TExampleEngineBaseTest) + protected + function CreateSolver: ISolver; override; + published + procedure TestPart2; + end; + implementation { TCosmicExpansionFullDataTestCase } @@ -58,6 +89,11 @@ begin AssertEquals(9686930, FSolver.GetResultPart1); end; +procedure TCosmicExpansionFullDataTestCase.TestPart2; +begin + AssertEquals(630728425490, FSolver.GetResultPart2); +end; + { TCosmicExpansionExampleTestCase } function TCosmicExpansionExampleTestCase.CreateSolver: ISolver; @@ -70,9 +106,49 @@ begin AssertEquals(374, FSolver.GetResultPart1); end; +{ TFactor10CosmicExpansion } + +function TFactor10CosmicExpansion.GetExpansionFactor: Integer; +begin + Result := 9; +end; + +{ TCosmicExpansionExampleFactor10TestCase } + +function TCosmicExpansionExampleFactor10TestCase.CreateSolver: ISolver; +begin + Result := TFactor10CosmicExpansion.Create; +end; + +procedure TCosmicExpansionExampleFactor10TestCase.TestPart2; +begin + AssertEquals(1030, FSolver.GetResultPart2); +end; + +{ TFactor100CosmicExpansion } + +function TFactor100CosmicExpansion.GetExpansionFactor: Integer; +begin + Result := 99; +end; + +{ TCosmicExpansionExampleFactor100TestCase } + +function TCosmicExpansionExampleFactor100TestCase.CreateSolver: ISolver; +begin + Result := TFactor100CosmicExpansion.Create; +end; + +procedure TCosmicExpansionExampleFactor100TestCase.TestPart2; +begin + AssertEquals(8410, FSolver.GetResultPart2); +end; + initialization RegisterTest(TCosmicExpansionFullDataTestCase); RegisterTest(TCosmicExpansionExampleTestCase); + RegisterTest(TCosmicExpansionExampleFactor10TestCase); + RegisterTest(TCosmicExpansionExampleFactor100TestCase); end.