fix: improve day 6 part 2 solution...moved from 40s to 15s by only looking at places on guard's existing path.
This commit is contained in:
@@ -28,14 +28,16 @@ class Map
|
|||||||
public int IdentifyNumberOfPlacementsForNewObstruction()
|
public int IdentifyNumberOfPlacementsForNewObstruction()
|
||||||
{
|
{
|
||||||
var count = 0;
|
var count = 0;
|
||||||
|
|
||||||
WalkMap((rowIndex, columnIndex) =>
|
var guardPathPositions = GetDistinctGuardPositions();
|
||||||
|
|
||||||
|
foreach (var (columnIndex, rowIndex) in guardPathPositions)
|
||||||
{
|
{
|
||||||
var currentCharacter = _input[rowIndex][columnIndex];
|
var currentCharacter = _input[rowIndex][columnIndex];
|
||||||
|
|
||||||
if (IsGuard(currentCharacter) || IsBlocked(currentCharacter))
|
if (IsGuard(currentCharacter) || IsBlocked(currentCharacter))
|
||||||
{
|
{
|
||||||
return;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
var mapWithNewObstruction = _input.ToArray();
|
var mapWithNewObstruction = _input.ToArray();
|
||||||
@@ -49,7 +51,7 @@ class Map
|
|||||||
{
|
{
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
@@ -95,9 +97,12 @@ class Map
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int PredictDistinctPositionsCount()
|
public int PredictDistinctPositionsCount() => GetDistinctGuardPositions().Count;
|
||||||
|
|
||||||
|
private HashSet<(int x, int y)> GetDistinctGuardPositions()
|
||||||
{
|
{
|
||||||
var currentGuardPosition = GetGuardPosition();
|
var currentGuardPosition = GetGuardPosition();
|
||||||
|
|
||||||
var uniquePositions = new HashSet<(int x, int y)>()
|
var uniquePositions = new HashSet<(int x, int y)>()
|
||||||
{
|
{
|
||||||
(currentGuardPosition.ColumnIndex, currentGuardPosition.RowIndex),
|
(currentGuardPosition.ColumnIndex, currentGuardPosition.RowIndex),
|
||||||
@@ -129,7 +134,7 @@ class Map
|
|||||||
uniquePositions.Add((currentGuardPosition.ColumnIndex, currentGuardPosition.RowIndex));
|
uniquePositions.Add((currentGuardPosition.ColumnIndex, currentGuardPosition.RowIndex));
|
||||||
}
|
}
|
||||||
|
|
||||||
return uniquePositions.Count;
|
return uniquePositions;
|
||||||
}
|
}
|
||||||
|
|
||||||
private GuardPosition GetGuardPosition()
|
private GuardPosition GetGuardPosition()
|
||||||
|
|||||||
Reference in New Issue
Block a user