almost got it
This commit is contained in:
parent
a91dd1c661
commit
c3eb1c24a5
@ -24,7 +24,7 @@ namespace AdventOfCode.Solutions._2023
|
|||||||
|
|
||||||
public async Task<string> GetSolutionPart2()
|
public async Task<string> GetSolutionPart2()
|
||||||
{
|
{
|
||||||
long maxCycles = 1_000_000_000;
|
int maxCycles = 1_000_000_000;
|
||||||
Dictionary<string, int> mapStates = [];
|
Dictionary<string, int> mapStates = [];
|
||||||
bool foundLoop = false;
|
bool foundLoop = false;
|
||||||
|
|
||||||
@ -32,25 +32,84 @@ namespace AdventOfCode.Solutions._2023
|
|||||||
HashSet<Point> bolders = grid.FindWithValue('O').Select(p => new Point(p.X, p.Y)).ToHashSet();
|
HashSet<Point> bolders = grid.FindWithValue('O').Select(p => new Point(p.X, p.Y)).ToHashSet();
|
||||||
HashSet<Point> cubes = grid.FindWithValue('#').Select(p => new Point(p.X, p.Y)).ToHashSet();
|
HashSet<Point> cubes = grid.FindWithValue('#').Select(p => new Point(p.X, p.Y)).ToHashSet();
|
||||||
|
|
||||||
|
int totalBolders = bolders.Count;
|
||||||
|
|
||||||
for (int currentCycle = 0; currentCycle < maxCycles; currentCycle++)
|
for (int currentCycle = 0; currentCycle < maxCycles; currentCycle++)
|
||||||
{
|
{
|
||||||
|
// ensure the state is saved
|
||||||
mapStates.TryAdd(CreateStringMap(bolders, cubes, grid.Rows, grid.Columns), currentCycle);
|
mapStates.TryAdd(CreateStringMap(bolders, cubes, grid.Rows, grid.Columns), currentCycle);
|
||||||
|
// add a test to see when we hit the right cycle for the test data
|
||||||
|
|
||||||
|
// move the boulders
|
||||||
bolders = MoveUp(bolders, cubes);
|
bolders = MoveUp(bolders, cubes);
|
||||||
|
if (bolders.Count != totalBolders)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"lost boulders MoveUp at {currentCycle}, from {totalBolders} to {bolders.Count}");
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
bolders = MoveLeft(bolders, cubes);
|
bolders = MoveLeft(bolders, cubes);
|
||||||
|
if (bolders.Count != totalBolders)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"lost boulders MoveLeft at {currentCycle}, from {totalBolders} to {bolders.Count}");
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
bolders = MoveDown(bolders, cubes, grid.Rows);
|
bolders = MoveDown(bolders, cubes, grid.Rows);
|
||||||
|
if (bolders.Count != totalBolders)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"lost boulders MoveDown at {currentCycle}, from {totalBolders} to {bolders.Count}");
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
bolders = MoveRight(bolders, cubes, grid.Columns);
|
bolders = MoveRight(bolders, cubes, grid.Columns);
|
||||||
|
|
||||||
|
if (bolders.Count != totalBolders)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"lost boulders MoveRight at {currentCycle}, from {totalBolders} to {bolders.Count}");
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
// see if we found the loop, if so we continue untill the end
|
||||||
if (foundLoop) continue;
|
if (foundLoop) continue;
|
||||||
|
|
||||||
int hash = bolders.GetHashCode();
|
// see if the current layout of the platform already exists
|
||||||
|
// if so we are repeating our selfs so wel can calculate the remaining steps to get to the end.
|
||||||
|
// we might even yoink the map from the dic and use that
|
||||||
if (mapStates.TryGetValue(CreateStringMap(bolders, cubes, grid.Rows, grid.Columns), out int prevCycle))
|
if (mapStates.TryGetValue(CreateStringMap(bolders, cubes, grid.Rows, grid.Columns), out int prevCycle))
|
||||||
{
|
{
|
||||||
// calculate remaining cycles to the first exit point
|
// calculate remaining cycles to the first exit point
|
||||||
long remainingCycles = (maxCycles - prevCycle) % (currentCycle + 1 - prevCycle) + 1;
|
int remainingCycles = maxCycles % (currentCycle + 1);
|
||||||
// set the total the first exit point
|
// set the total the first exit point
|
||||||
maxCycles = remainingCycles + currentCycle;
|
int exitMap = remainingCycles + currentCycle;
|
||||||
foundLoop = true;
|
|
||||||
|
string[] lines = mapStates.First(kvp => kvp.Value == prevCycle - 1).Key.Split(':', StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
int totalWeight = 0;
|
||||||
|
for (int lineIndex = 0; lineIndex < lines.Length; lineIndex++)
|
||||||
|
{
|
||||||
|
int weight = lines[lineIndex].Length, lineWeight = 0;
|
||||||
|
for (int charIndex = 0; charIndex < lines[lineIndex].Length; charIndex++)
|
||||||
|
{
|
||||||
|
if (lines[lineIndex][charIndex] is '.')
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (lines[lineIndex][charIndex] is '#')
|
||||||
|
{
|
||||||
|
weight = lines[lineIndex].Length - charIndex - 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lines[lineIndex][charIndex] is 'O')
|
||||||
|
{
|
||||||
|
lineWeight += weight;
|
||||||
|
weight--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
totalWeight += lineWeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
return totalWeight.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,7 +141,7 @@ namespace AdventOfCode.Solutions._2023
|
|||||||
private HashSet<Point> MoveDown(HashSet<Point> bolders, HashSet<Point> cubes, long ySize)
|
private HashSet<Point> MoveDown(HashSet<Point> bolders, HashSet<Point> cubes, long ySize)
|
||||||
{
|
{
|
||||||
HashSet<Point> boldersMoved = [];
|
HashSet<Point> boldersMoved = [];
|
||||||
foreach (Point bolder in bolders.OrderBy(bolder => bolder.Y))
|
foreach (Point bolder in bolders.OrderByDescending(bolder => bolder.Y))
|
||||||
{
|
{
|
||||||
// current location
|
// current location
|
||||||
long ySearch = bolder.Y;
|
long ySearch = bolder.Y;
|
||||||
@ -108,8 +167,8 @@ namespace AdventOfCode.Solutions._2023
|
|||||||
// current location
|
// current location
|
||||||
long xSearch = bolder.X;
|
long xSearch = bolder.X;
|
||||||
while (xSearch - 1 >= 0 // ensure in map
|
while (xSearch - 1 >= 0 // ensure in map
|
||||||
&& !boldersMoved.Contains(new(xSearch, bolder.Y)) // check that the next tile does not have a bolder
|
&& !boldersMoved.Contains(new(xSearch - 1, bolder.Y)) // check that the next tile does not have a bolder
|
||||||
&& !cubes.Contains(new(xSearch, bolder.Y))) // check that the next tile has not cube
|
&& !cubes.Contains(new(xSearch - 1, bolder.Y))) // check that the next tile has not cube
|
||||||
{
|
{
|
||||||
xSearch--; // move to next
|
xSearch--; // move to next
|
||||||
}
|
}
|
||||||
@ -124,13 +183,13 @@ namespace AdventOfCode.Solutions._2023
|
|||||||
private HashSet<Point> MoveRight(HashSet<Point> bolders, HashSet<Point> cubes, long xSize)
|
private HashSet<Point> MoveRight(HashSet<Point> bolders, HashSet<Point> cubes, long xSize)
|
||||||
{
|
{
|
||||||
HashSet<Point> boldersMoved = [];
|
HashSet<Point> boldersMoved = [];
|
||||||
foreach (Point bolder in bolders.OrderBy(bolder => bolder.Y))
|
foreach (Point bolder in bolders.OrderByDescending(bolder => bolder.X))
|
||||||
{
|
{
|
||||||
// current location
|
// current location
|
||||||
long xSearch = bolder.Y;
|
long xSearch = bolder.X;
|
||||||
while (xSearch + 1 < xSize // ensure in map
|
while (xSearch + 1 < xSize // ensure in map
|
||||||
&& !boldersMoved.Contains(new(xSearch, bolder.Y)) // check that the next tile does not have a bolder
|
&& !boldersMoved.Contains(new(xSearch + 1, bolder.Y)) // check that the next tile does not have a bolder
|
||||||
&& !cubes.Contains(new(xSearch, bolder.Y))) // check that the next tile has not cube
|
&& !cubes.Contains(new(xSearch + 1, bolder.Y))) // check that the next tile has not cube
|
||||||
{
|
{
|
||||||
xSearch++; // move to next
|
xSearch++; // move to next
|
||||||
}
|
}
|
||||||
@ -157,7 +216,7 @@ namespace AdventOfCode.Solutions._2023
|
|||||||
stringBuilder.Append('.');
|
stringBuilder.Append('.');
|
||||||
}
|
}
|
||||||
|
|
||||||
stringBuilder.AppendLine();
|
stringBuilder.Append(':');
|
||||||
}
|
}
|
||||||
|
|
||||||
return stringBuilder.ToString();
|
return stringBuilder.ToString();
|
||||||
|
|||||||
@ -12,6 +12,9 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Update="2023\Day 15\Day15.cs">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</Compile>
|
||||||
<Compile Update="2023\Day 13\Day13.cs">
|
<Compile Update="2023\Day 13\Day13.cs">
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user