style: refactoring based on Rider suggestions
This commit is contained in:
@@ -2,20 +2,13 @@ namespace RedNosedReports;
|
||||
|
||||
class Report(List<int> levels)
|
||||
{
|
||||
private readonly List<int> _levels = levels;
|
||||
|
||||
public string GetDebugOutput()
|
||||
{
|
||||
return $"{string.Join(' ', _levels)} {IsSafe()} {IsSafeWithProblemDampener()}";
|
||||
}
|
||||
|
||||
public bool IsSafe()
|
||||
{
|
||||
for (var i = 0; i < _levels.Count - 1; i++)
|
||||
for (var i = 0; i < levels.Count - 1; i++)
|
||||
{
|
||||
var current = _levels[i];
|
||||
var next = _levels[i + 1];
|
||||
|
||||
var current = levels[i];
|
||||
var next = levels[i + 1];
|
||||
|
||||
if (HasExceededLimits(current, next) || HasChangedDirection(current, next))
|
||||
{
|
||||
return false;
|
||||
@@ -27,11 +20,11 @@ class Report(List<int> levels)
|
||||
|
||||
public bool IsSafeWithProblemDampener()
|
||||
{
|
||||
for (var currentIndex = 0; currentIndex < _levels.Count - 1; currentIndex++)
|
||||
for (var currentIndex = 0; currentIndex < levels.Count - 1; currentIndex++)
|
||||
{
|
||||
var nextIndex = currentIndex + 1;
|
||||
var current = _levels[currentIndex];
|
||||
var next = _levels[nextIndex];
|
||||
var current = levels[currentIndex];
|
||||
var next = levels[nextIndex];
|
||||
var directionChanged = HasChangedDirection(current, next);
|
||||
|
||||
// problem dampener permits us to try to make
|
||||
@@ -41,87 +34,74 @@ class Report(List<int> levels)
|
||||
// the limits we are going to attempt to see if
|
||||
// the levels will work if we remove one of the levels in
|
||||
// the current pair.
|
||||
if (HasExceededLimits(current, next) || directionChanged)
|
||||
if (HasExceededLimits(current, next) is false && directionChanged is false)
|
||||
{
|
||||
// specific edge case where direction changes
|
||||
// on the second pair of levels after the first
|
||||
// pair is a valid.
|
||||
// i.e. 43, 41, 43, 44, 45, 47, 49
|
||||
// in this case we should attempt to make levels
|
||||
// work by removing first element so that we can
|
||||
// essentially reset what the valid direction is.
|
||||
if (directionChanged && currentIndex is 1)
|
||||
{
|
||||
var levelsWithoutFirstElement = _levels.ToList();
|
||||
levelsWithoutFirstElement.RemoveAt(0);
|
||||
var isSafeWithoutFirstElement = new Report(levelsWithoutFirstElement).IsSafe();
|
||||
|
||||
if (isSafeWithoutFirstElement)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// check if levels work without the current level
|
||||
var levelsWithoutCurrent = _levels.ToList();
|
||||
levelsWithoutCurrent.RemoveAt(currentIndex);
|
||||
var isSafeWithoutCurrent = new Report(levelsWithoutCurrent).IsSafe();
|
||||
|
||||
if (isSafeWithoutCurrent)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// check if levels work without the next level
|
||||
var levelsWithoutNext = _levels.ToList();
|
||||
levelsWithoutNext.RemoveAt(nextIndex);
|
||||
var isSafeWithoutNext = new Report(levelsWithoutNext).IsSafe();
|
||||
|
||||
if (isSafeWithoutNext)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// if won't work with all levels
|
||||
// or without current or next then doesn't
|
||||
// work at all.
|
||||
return false;
|
||||
continue;
|
||||
}
|
||||
|
||||
// specific edge case where direction changes
|
||||
// on the second pair of levels after the first
|
||||
// pair is a valid.
|
||||
// i.e. 43, 41, 43, 44, 45, 47, 49
|
||||
// in this case we should attempt to make levels
|
||||
// work by removing first element so that we can
|
||||
// essentially reset what the valid direction is.
|
||||
if (directionChanged && currentIndex is 1)
|
||||
{
|
||||
var levelsWithoutFirstElement = levels.ToList();
|
||||
levelsWithoutFirstElement.RemoveAt(0);
|
||||
var isSafeWithoutFirstElement = new Report(levelsWithoutFirstElement).IsSafe();
|
||||
|
||||
if (isSafeWithoutFirstElement)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// check if levels work without the current level
|
||||
var levelsWithoutCurrent = levels.ToList();
|
||||
levelsWithoutCurrent.RemoveAt(currentIndex);
|
||||
var isSafeWithoutCurrent = new Report(levelsWithoutCurrent).IsSafe();
|
||||
|
||||
if (isSafeWithoutCurrent)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// check if levels work without the next level
|
||||
var levelsWithoutNext = levels.ToList();
|
||||
levelsWithoutNext.RemoveAt(nextIndex);
|
||||
var isSafeWithoutNext = new Report(levelsWithoutNext).IsSafe();
|
||||
|
||||
// we've exhausted ways that the levels might work
|
||||
// so we can just return this check.
|
||||
return isSafeWithoutNext;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool HasExceededLimits(int current, int next)
|
||||
private static bool HasExceededLimits(int current, int next)
|
||||
{
|
||||
var delta = Math.Abs(next - current);
|
||||
|
||||
if (delta > 3 || delta < 1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return delta is > 3 or < 1;
|
||||
}
|
||||
|
||||
private bool HasChangedDirection(int current, int next)
|
||||
{
|
||||
var direction = _levels[1] > _levels[0]
|
||||
var direction = levels[1] > levels[0]
|
||||
? Direction.Increasing
|
||||
: Direction.Decreasing;
|
||||
|
||||
var isNextGreaterThanCurrent = next > current;
|
||||
|
||||
if (direction is Direction.Increasing && isNextGreaterThanCurrent is false)
|
||||
switch (direction)
|
||||
{
|
||||
return true;
|
||||
case Direction.Increasing when isNextGreaterThanCurrent is false:
|
||||
case Direction.Decreasing when isNextGreaterThanCurrent:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
if (direction is Direction.Decreasing && isNextGreaterThanCurrent)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user