style: refactoring based on Rider suggestions

This commit is contained in:
Stevan Freeborn
2024-12-03 12:19:34 -06:00
parent e2ea828222
commit 597e08024f
4 changed files with 62 additions and 89 deletions
+2
View File
@@ -1,3 +1,5 @@
namespace HistorianHysteria;
class PuzzleSolver class PuzzleSolver
{ {
public int CalculateTotalDistance(List<int> left, List<int> right) public int CalculateTotalDistance(List<int> left, List<int> right)
-1
View File
@@ -2,7 +2,6 @@ namespace RedNosedReports;
static class Direction static class Direction
{ {
public const string Unknown = "Unknown";
public const string Increasing = "Increasing"; public const string Increasing = "Increasing";
public const string Decreasing = "Decreasing"; public const string Decreasing = "Decreasing";
} }
+4 -12
View File
@@ -2,16 +2,8 @@ namespace RedNosedReports;
class PuzzleParser class PuzzleParser
{ {
public List<Report> Parse(string[] lines) public List<Report> Parse(string[] lines) => lines
{ .Select(line => line.Split(' ').Select(int.Parse).ToList())
var reports = new List<Report>(); .Select(numbers => new Report(numbers))
.ToList();
foreach (var line in lines)
{
var numbers = line.Split(' ').Select(int.Parse).ToList();
reports.Add(new Report(numbers));
}
return reports;
}
} }
+24 -44
View File
@@ -2,19 +2,12 @@ namespace RedNosedReports;
class Report(List<int> levels) class Report(List<int> levels)
{ {
private readonly List<int> _levels = levels;
public string GetDebugOutput()
{
return $"{string.Join(' ', _levels)} {IsSafe()} {IsSafeWithProblemDampener()}";
}
public bool IsSafe() 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 current = levels[i];
var next = _levels[i + 1]; var next = levels[i + 1];
if (HasExceededLimits(current, next) || HasChangedDirection(current, next)) if (HasExceededLimits(current, next) || HasChangedDirection(current, next))
{ {
@@ -27,11 +20,11 @@ class Report(List<int> levels)
public bool IsSafeWithProblemDampener() 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 nextIndex = currentIndex + 1;
var current = _levels[currentIndex]; var current = levels[currentIndex];
var next = _levels[nextIndex]; var next = levels[nextIndex];
var directionChanged = HasChangedDirection(current, next); var directionChanged = HasChangedDirection(current, next);
// problem dampener permits us to try to make // problem dampener permits us to try to make
@@ -41,8 +34,11 @@ class Report(List<int> levels)
// the limits we are going to attempt to see if // the limits we are going to attempt to see if
// the levels will work if we remove one of the levels in // the levels will work if we remove one of the levels in
// the current pair. // the current pair.
if (HasExceededLimits(current, next) || directionChanged) if (HasExceededLimits(current, next) is false && directionChanged is false)
{ {
continue;
}
// specific edge case where direction changes // specific edge case where direction changes
// on the second pair of levels after the first // on the second pair of levels after the first
// pair is a valid. // pair is a valid.
@@ -52,7 +48,7 @@ class Report(List<int> levels)
// essentially reset what the valid direction is. // essentially reset what the valid direction is.
if (directionChanged && currentIndex is 1) if (directionChanged && currentIndex is 1)
{ {
var levelsWithoutFirstElement = _levels.ToList(); var levelsWithoutFirstElement = levels.ToList();
levelsWithoutFirstElement.RemoveAt(0); levelsWithoutFirstElement.RemoveAt(0);
var isSafeWithoutFirstElement = new Report(levelsWithoutFirstElement).IsSafe(); var isSafeWithoutFirstElement = new Report(levelsWithoutFirstElement).IsSafe();
@@ -63,7 +59,7 @@ class Report(List<int> levels)
} }
// check if levels work without the current level // check if levels work without the current level
var levelsWithoutCurrent = _levels.ToList(); var levelsWithoutCurrent = levels.ToList();
levelsWithoutCurrent.RemoveAt(currentIndex); levelsWithoutCurrent.RemoveAt(currentIndex);
var isSafeWithoutCurrent = new Report(levelsWithoutCurrent).IsSafe(); var isSafeWithoutCurrent = new Report(levelsWithoutCurrent).IsSafe();
@@ -73,55 +69,39 @@ class Report(List<int> levels)
} }
// check if levels work without the next level // check if levels work without the next level
var levelsWithoutNext = _levels.ToList(); var levelsWithoutNext = levels.ToList();
levelsWithoutNext.RemoveAt(nextIndex); levelsWithoutNext.RemoveAt(nextIndex);
var isSafeWithoutNext = new Report(levelsWithoutNext).IsSafe(); var isSafeWithoutNext = new Report(levelsWithoutNext).IsSafe();
if (isSafeWithoutNext) // we've exhausted ways that the levels might work
{ // so we can just return this check.
return true; return isSafeWithoutNext;
}
// if won't work with all levels
// or without current or next then doesn't
// work at all.
return false;
}
} }
return true; return true;
} }
private bool HasExceededLimits(int current, int next) private static bool HasExceededLimits(int current, int next)
{ {
var delta = Math.Abs(next - current); var delta = Math.Abs(next - current);
return delta is > 3 or < 1;
if (delta > 3 || delta < 1)
{
return true;
}
return false;
} }
private bool HasChangedDirection(int current, int next) private bool HasChangedDirection(int current, int next)
{ {
var direction = _levels[1] > _levels[0] var direction = levels[1] > levels[0]
? Direction.Increasing ? Direction.Increasing
: Direction.Decreasing; : Direction.Decreasing;
var isNextGreaterThanCurrent = next > current; var isNextGreaterThanCurrent = next > current;
if (direction is Direction.Increasing && isNextGreaterThanCurrent is false) switch (direction)
{ {
case Direction.Increasing when isNextGreaterThanCurrent is false:
case Direction.Decreasing when isNextGreaterThanCurrent:
return true; return true;
} default:
if (direction is Direction.Decreasing && isNextGreaterThanCurrent)
{
return true;
}
return false; return false;
} }
} }
}