diff --git a/01/HistorianHysteria/PuzzleSolver.cs b/01/HistorianHysteria/PuzzleSolver.cs index 4a79f0d..4e5f01c 100644 --- a/01/HistorianHysteria/PuzzleSolver.cs +++ b/01/HistorianHysteria/PuzzleSolver.cs @@ -1,3 +1,5 @@ +namespace HistorianHysteria; + class PuzzleSolver { public int CalculateTotalDistance(List left, List right) diff --git a/02/RedNosedReports/Direction.cs b/02/RedNosedReports/Direction.cs index 3ced75a..6d62e4a 100644 --- a/02/RedNosedReports/Direction.cs +++ b/02/RedNosedReports/Direction.cs @@ -2,7 +2,6 @@ namespace RedNosedReports; static class Direction { - public const string Unknown = "Unknown"; public const string Increasing = "Increasing"; public const string Decreasing = "Decreasing"; } \ No newline at end of file diff --git a/02/RedNosedReports/PuzzleParser.cs b/02/RedNosedReports/PuzzleParser.cs index 09f34b8..0e85811 100644 --- a/02/RedNosedReports/PuzzleParser.cs +++ b/02/RedNosedReports/PuzzleParser.cs @@ -2,16 +2,8 @@ namespace RedNosedReports; class PuzzleParser { - public List Parse(string[] lines) - { - var reports = new List(); - - foreach (var line in lines) - { - var numbers = line.Split(' ').Select(int.Parse).ToList(); - reports.Add(new Report(numbers)); - } - - return reports; - } + public List Parse(string[] lines) => lines + .Select(line => line.Split(' ').Select(int.Parse).ToList()) + .Select(numbers => new Report(numbers)) + .ToList(); } diff --git a/02/RedNosedReports/Report.cs b/02/RedNosedReports/Report.cs index 9e5e30e..d1848f5 100644 --- a/02/RedNosedReports/Report.cs +++ b/02/RedNosedReports/Report.cs @@ -2,20 +2,13 @@ namespace RedNosedReports; class Report(List levels) { - private readonly List _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 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 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; } }