Added solution for "Day 11: Cosmic Expansion", part 2

This commit is contained in:
Stefan Müller 2023-12-11 16:45:56 +01:00 committed by Stefan Müller
parent 5bd814a381
commit 8b13ad992b
3 changed files with 100 additions and 4 deletions

View File

@ -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. 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
<https://adventofcode.com/2023/day/11>
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 ## License
Copyright (C) 2023 Stefan Müller Copyright (C) 2023 Stefan Müller

View File

@ -26,8 +26,8 @@ uses
const const
CGalaxyChar = '#'; CGalaxyChar = '#';
CEmptySpaceFactor = 2; CEmptySpaceFactor = 1;
CNonEmptySpaceFactor = 1; CNonEmptySpaceFactor = 0;
type type
@ -45,6 +45,7 @@ type
procedure Finish; override; procedure Finish; override;
function GetDataFileName: string; override; function GetDataFileName: string; override;
function GetPuzzleName: string; override; function GetPuzzleName: string; override;
function GetExpansionFactor: Integer; virtual;
end; end;
implementation implementation
@ -107,9 +108,15 @@ begin
for i := 0 to FGalaxies.Count - 1 do for i := 0 to FGalaxies.Count - 1 do
for j := i + 1 to FGalaxies.Count - 1 do begin 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 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 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;
end; end;
@ -123,5 +130,10 @@ begin
Result := 'Day 11: Cosmic Expansion'; Result := 'Day 11: Cosmic Expansion';
end; end;
function TCosmicExpansion.GetExpansionFactor: Integer;
begin
Result := 999999;
end;
end. end.

View File

@ -33,6 +33,7 @@ type
function CreateSolver: ISolver; override; function CreateSolver: ISolver; override;
published published
procedure TestPart1; procedure TestPart1;
procedure TestPart2;
end; end;
{ TCosmicExpansionExampleTestCase } { TCosmicExpansionExampleTestCase }
@ -44,6 +45,36 @@ type
procedure TestPart1; procedure TestPart1;
end; 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 implementation
{ TCosmicExpansionFullDataTestCase } { TCosmicExpansionFullDataTestCase }
@ -58,6 +89,11 @@ begin
AssertEquals(9686930, FSolver.GetResultPart1); AssertEquals(9686930, FSolver.GetResultPart1);
end; end;
procedure TCosmicExpansionFullDataTestCase.TestPart2;
begin
AssertEquals(630728425490, FSolver.GetResultPart2);
end;
{ TCosmicExpansionExampleTestCase } { TCosmicExpansionExampleTestCase }
function TCosmicExpansionExampleTestCase.CreateSolver: ISolver; function TCosmicExpansionExampleTestCase.CreateSolver: ISolver;
@ -70,9 +106,49 @@ begin
AssertEquals(374, FSolver.GetResultPart1); AssertEquals(374, FSolver.GetResultPart1);
end; 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 initialization
RegisterTest(TCosmicExpansionFullDataTestCase); RegisterTest(TCosmicExpansionFullDataTestCase);
RegisterTest(TCosmicExpansionExampleTestCase); RegisterTest(TCosmicExpansionExampleTestCase);
RegisterTest(TCosmicExpansionExampleFactor10TestCase);
RegisterTest(TCosmicExpansionExampleFactor100TestCase);
end. end.