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;
}
} }
+56 -76
View File
@@ -2,20 +2,13 @@ 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))
{ {
return false; return false;
@@ -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,87 +34,74 @@ 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)
{ {
// specific edge case where direction changes continue;
// 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;
} }
// 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; 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)
{ {
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;
} }
} }