Day2 updated attempt
parent
95702c7a3e
commit
0aa8bb80ba
283
Advent24/Day2.cs
283
Advent24/Day2.cs
|
@ -9,10 +9,108 @@ namespace Advent24
|
|||
{
|
||||
internal class Day2
|
||||
{
|
||||
private static Boolean ReportIsSafe(String[] levelsArray, Int32? levelIndexToSkip = null)
|
||||
{
|
||||
Int32 currentLevel;
|
||||
Int32? previousLevel = null;
|
||||
Int32? previousPreviousLevel = null;
|
||||
Int32 differenceInLevels = 0;
|
||||
Boolean? isIncreasing = null;
|
||||
Boolean reportIsSafe = true;
|
||||
|
||||
Int32 currentLevelIndex = 0;
|
||||
|
||||
foreach (var level in levelsArray)
|
||||
{
|
||||
Console.WriteLine("{0:s} {1:d}", " level = ", level);
|
||||
|
||||
// if there is no elementIndexToSkip, assume sentinel value -1
|
||||
// so that it never matches the currentElementIndex
|
||||
if((levelIndexToSkip ?? -1) == currentLevelIndex)
|
||||
{
|
||||
// assume current element doesn't exist, so the previous
|
||||
// element should be the one that was before it
|
||||
previousLevel = previousPreviousLevel;
|
||||
|
||||
Console.WriteLine("{0:s} {1:d}", " currentLevelIndex (skipped) = ", currentLevelIndex);
|
||||
|
||||
// if (currentLevelIndex != 0) Console.ReadKey();
|
||||
|
||||
// increment the currentElementIndex and short-circuit the loop
|
||||
currentLevelIndex++;
|
||||
continue;
|
||||
}
|
||||
|
||||
currentLevel = System.Convert.ToInt32(level);
|
||||
|
||||
if (currentLevel > (previousLevel ?? 0))
|
||||
{
|
||||
// first time, so can't tell trend
|
||||
if (!previousLevel.HasValue) isIncreasing = null;
|
||||
else if ((!isIncreasing) ?? false)
|
||||
{
|
||||
// level was decreasing but is now increasing, so report is unsafe
|
||||
reportIsSafe = false;
|
||||
break;
|
||||
}
|
||||
else isIncreasing = true;
|
||||
differenceInLevels = currentLevel - (previousLevel ?? 0);
|
||||
}
|
||||
else if (currentLevel < (previousLevel ?? 0))
|
||||
{
|
||||
// first time, so can't tell trend
|
||||
if (!previousLevel.HasValue) isIncreasing = null;
|
||||
else if (isIncreasing ?? false)
|
||||
{
|
||||
// level was increasing but is now decreasing, so report is unsafe
|
||||
reportIsSafe = false;
|
||||
break;
|
||||
}
|
||||
else isIncreasing = false;
|
||||
differenceInLevels = (previousLevel ?? 0) - currentLevel;
|
||||
}
|
||||
else if (previousLevel.HasValue)
|
||||
{
|
||||
// both currentLevel and previousLevel are equal, so report is unsafe
|
||||
reportIsSafe = false;
|
||||
break;
|
||||
}
|
||||
|
||||
Console.WriteLine("{0:s} {1:d}", " differenceInLevels = ", differenceInLevels);
|
||||
Console.WriteLine("{0:s} {1:d}", " isIncreasing = ", isIncreasing);
|
||||
|
||||
// control reaches this line if currentLevel = previousLevel
|
||||
// or if it was previously increasing/decreasing and is also
|
||||
// currently increasing/decreasing
|
||||
|
||||
// confirm that it isn't first time, so a difference in levels is conceptually valid
|
||||
if (previousLevel.HasValue)
|
||||
{
|
||||
// difference in levels is outside the safe band, so report is unsafe
|
||||
if (differenceInLevels < 1 || differenceInLevels > 3)
|
||||
{
|
||||
reportIsSafe = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// control reaches this line if report has not been proven unsafe yet
|
||||
|
||||
// updating previousLevel and previousPreviousLevel in preparation for the next iteration
|
||||
previousPreviousLevel = previousLevel;
|
||||
previousLevel = currentLevel;
|
||||
|
||||
// incrementing the current-element-number indicator
|
||||
currentLevelIndex++;
|
||||
}
|
||||
|
||||
return reportIsSafe;
|
||||
}
|
||||
|
||||
public Day2()
|
||||
{
|
||||
string fileData = System.IO.File.ReadAllText(@"..\..\..\inputd2.txt");
|
||||
var reportsArray = fileData.Split('\n');
|
||||
var reportsArray = fileData.Split('\n', StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
Int32 numberOfSafeReports = 0;
|
||||
|
||||
|
@ -25,177 +123,26 @@ namespace Advent24
|
|||
|
||||
var levelsArray = report.Split(' ');
|
||||
|
||||
Int32 currentLevel = 0;
|
||||
Int32? previousLevel = null;
|
||||
Int32? previousPreviousLevel = null;
|
||||
Int32? discardedLevel = null;
|
||||
Int32 differenceInLevels = 0;
|
||||
Boolean? isIncreasing = null;
|
||||
Boolean reportIsSafe = true;
|
||||
Boolean problemDampenerEnabled = true;
|
||||
Int32 numberOfLevels = levelsArray.Length;
|
||||
Boolean reportIsSafe = false;
|
||||
|
||||
foreach (var level in levelsArray)
|
||||
// iterate through each index to check subreports
|
||||
// for safety
|
||||
for (Int32 i = 0; i < numberOfLevels; i++)
|
||||
{
|
||||
Console.WriteLine("{0:s} {1:d}", " level = ", level);
|
||||
// check if the current subreport is safe
|
||||
reportIsSafe = ReportIsSafe(levelsArray, i);
|
||||
|
||||
currentLevel = System.Convert.ToInt32(level);
|
||||
// if any subreport is safe, the report is safe,
|
||||
// so we don't need to check any of the other
|
||||
// subreports and can break
|
||||
if (reportIsSafe) break;
|
||||
}
|
||||
|
||||
RestartIteration:
|
||||
|
||||
if (currentLevel > (previousLevel ?? 0))
|
||||
{
|
||||
// first time, so can't tell trend
|
||||
if (!previousLevel.HasValue) isIncreasing = null;
|
||||
else if ((!isIncreasing) ?? false)
|
||||
{
|
||||
if (problemDampenerEnabled)
|
||||
{
|
||||
// store previous level in discardedLevel to instead be
|
||||
// able to assume the previous level doesn't exist - only
|
||||
// if this is the second level
|
||||
discardedLevel = previousLevel;
|
||||
|
||||
// assume the current level didn't exist
|
||||
previousLevel = previousPreviousLevel;
|
||||
|
||||
Console.WriteLine("{0:s} {1:d}", " problemDampenerActivated: new previousLevel = ", previousLevel);
|
||||
Console.ReadLine();
|
||||
|
||||
// problemDampener used, so disabled for the rest of the report
|
||||
problemDampenerEnabled = false;
|
||||
|
||||
// previous level had a value and previousPreviousLevel didn't,
|
||||
// so this is the second level, so we assume the previous level
|
||||
// doesn't exist and rerun this iteration
|
||||
if(discardedLevel.HasValue && !previousPreviousLevel.HasValue)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// continue to the next iteration
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
// level was decreasing but is now increasing, so report is unsafe
|
||||
reportIsSafe = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else isIncreasing = true;
|
||||
differenceInLevels = currentLevel - (previousLevel ?? 0);
|
||||
}
|
||||
else if (currentLevel < (previousLevel ?? 0))
|
||||
{
|
||||
// first time, so can't tell trend
|
||||
if (!previousLevel.HasValue) isIncreasing = null;
|
||||
else if (isIncreasing ?? false)
|
||||
{
|
||||
if (problemDampenerEnabled)
|
||||
{
|
||||
// store previous level in discardedLevel to instead be
|
||||
// able to assume the previous level doesn't exist - only
|
||||
// if this is the second level
|
||||
discardedLevel = previousLevel;
|
||||
|
||||
// assume the previous level didn't exist
|
||||
previousLevel = previousPreviousLevel;
|
||||
|
||||
Console.WriteLine("{0:s} {1:d}", " problemDampenerActivated: new previousLevel = ", previousLevel);
|
||||
Console.ReadLine();
|
||||
|
||||
// problemDampener used, so disabled for the rest of the report
|
||||
problemDampenerEnabled = false;
|
||||
|
||||
// continue to the next iteration
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
// level was increasing but is now decreasing, so report is unsafe
|
||||
reportIsSafe = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else isIncreasing = false;
|
||||
differenceInLevels = (previousLevel ?? 0) - currentLevel;
|
||||
}
|
||||
else if(previousLevel.HasValue)
|
||||
{
|
||||
if (problemDampenerEnabled)
|
||||
{
|
||||
// store previous level in discardedLevel to instead be
|
||||
// able to assume the previous level doesn't exist - only
|
||||
// if this is the second level
|
||||
discardedLevel = previousLevel;
|
||||
|
||||
// assume previous level didn't exist
|
||||
previousLevel = previousPreviousLevel;
|
||||
|
||||
Console.WriteLine("{0:s} {1:d}", " problemDampenerActivated: new previousLevel = ", previousLevel);
|
||||
Console.ReadLine();
|
||||
|
||||
// problemDampener used, so disabled for the rest of the report
|
||||
problemDampenerEnabled = false;
|
||||
|
||||
// continue to the next iteration
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
// both currentLevel and previousLevel are equal, so report is unsafe
|
||||
reportIsSafe = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("{0:s} {1:d}", " differenceInLevels = ", differenceInLevels);
|
||||
Console.WriteLine("{0:s} {1:d}", " isIncreasing = ", isIncreasing);
|
||||
|
||||
// control reaches this line if currentLevel = previousLevel
|
||||
// or if it was previously increasing/decreasing and is also
|
||||
// currently increasing/decreasing
|
||||
|
||||
// confirm that it isn't first time, so a difference in levels is conceptually valid
|
||||
if (previousLevel.HasValue)
|
||||
{
|
||||
// difference in levels is outside the safe band, so report is unsafe
|
||||
if (differenceInLevels < 1 || differenceInLevels > 3)
|
||||
{
|
||||
if (problemDampenerEnabled)
|
||||
{
|
||||
// store previous level in discardedLevel to instead be
|
||||
// able to assume the previous level doesn't exist - only
|
||||
// if this is the second level
|
||||
discardedLevel = previousLevel;
|
||||
|
||||
// assume the previous level didn't exist
|
||||
previousLevel = previousPreviousLevel;
|
||||
|
||||
Console.WriteLine("{0:s} {1:d}", " problemDampenerActivated: new previousLevel = ", previousLevel);
|
||||
Console.ReadLine();
|
||||
|
||||
// problemDampener used, so disabled for the rest of the report
|
||||
problemDampenerEnabled = false;
|
||||
|
||||
// continue to the next iteration
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
reportIsSafe = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// control reaches this line if report has not been proven unsafe yet
|
||||
|
||||
// updating previousPreviousLevel in preparation for the next iteration
|
||||
// if problemDampener is still enabled
|
||||
if(problemDampenerEnabled) previousPreviousLevel = previousLevel;
|
||||
// updating previousLevel in preparation for the next iteration
|
||||
previousLevel = currentLevel;
|
||||
// check full report for safety if report is still not proven safe
|
||||
if (!reportIsSafe)
|
||||
{
|
||||
reportIsSafe = ReportIsSafe(levelsArray);
|
||||
}
|
||||
|
||||
Console.WriteLine("{0:s} {1:d}", " reportIsSafe = ", reportIsSafe);
|
||||
|
|
|
@ -997,4 +997,4 @@
|
|||
77 79 81 82 85 86
|
||||
59 61 62 65 67 68
|
||||
94 93 92 91 90 87 85 83
|
||||
50 52 53 54 56 57 58 61
|
||||
50 52 53 54 56 57 58 61
|
Loading…
Reference in New Issue