Day2 updated attempt
parent
95702c7a3e
commit
0aa8bb80ba
187
Advent24/Day2.cs
187
Advent24/Day2.cs
|
@ -9,79 +9,50 @@ namespace Advent24
|
||||||
{
|
{
|
||||||
internal class Day2
|
internal class Day2
|
||||||
{
|
{
|
||||||
public Day2()
|
private static Boolean ReportIsSafe(String[] levelsArray, Int32? levelIndexToSkip = null)
|
||||||
{
|
{
|
||||||
string fileData = System.IO.File.ReadAllText(@"..\..\..\inputd2.txt");
|
Int32 currentLevel;
|
||||||
var reportsArray = fileData.Split('\n');
|
|
||||||
|
|
||||||
Int32 numberOfSafeReports = 0;
|
|
||||||
|
|
||||||
|
|
||||||
foreach (var report in reportsArray)
|
|
||||||
{
|
|
||||||
if (System.String.IsNullOrEmpty(report)) continue;
|
|
||||||
|
|
||||||
Console.WriteLine("{0:s} {1:d}", "report = ", report);
|
|
||||||
|
|
||||||
var levelsArray = report.Split(' ');
|
|
||||||
|
|
||||||
Int32 currentLevel = 0;
|
|
||||||
Int32? previousLevel = null;
|
Int32? previousLevel = null;
|
||||||
Int32? previousPreviousLevel = null;
|
Int32? previousPreviousLevel = null;
|
||||||
Int32? discardedLevel = null;
|
|
||||||
Int32 differenceInLevels = 0;
|
Int32 differenceInLevels = 0;
|
||||||
Boolean? isIncreasing = null;
|
Boolean? isIncreasing = null;
|
||||||
Boolean reportIsSafe = true;
|
Boolean reportIsSafe = true;
|
||||||
Boolean problemDampenerEnabled = true;
|
|
||||||
|
Int32 currentLevelIndex = 0;
|
||||||
|
|
||||||
foreach (var level in levelsArray)
|
foreach (var level in levelsArray)
|
||||||
{
|
{
|
||||||
Console.WriteLine("{0:s} {1:d}", " level = ", level);
|
Console.WriteLine("{0:s} {1:d}", " level = ", level);
|
||||||
|
|
||||||
currentLevel = System.Convert.ToInt32(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;
|
||||||
|
|
||||||
RestartIteration:
|
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))
|
if (currentLevel > (previousLevel ?? 0))
|
||||||
{
|
{
|
||||||
// first time, so can't tell trend
|
// first time, so can't tell trend
|
||||||
if (!previousLevel.HasValue) isIncreasing = null;
|
if (!previousLevel.HasValue) isIncreasing = null;
|
||||||
else if ((!isIncreasing) ?? false)
|
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
|
// level was decreasing but is now increasing, so report is unsafe
|
||||||
reportIsSafe = false;
|
reportIsSafe = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else isIncreasing = true;
|
else isIncreasing = true;
|
||||||
differenceInLevels = currentLevel - (previousLevel ?? 0);
|
differenceInLevels = currentLevel - (previousLevel ?? 0);
|
||||||
}
|
}
|
||||||
|
@ -90,64 +61,20 @@ namespace Advent24
|
||||||
// first time, so can't tell trend
|
// first time, so can't tell trend
|
||||||
if (!previousLevel.HasValue) isIncreasing = null;
|
if (!previousLevel.HasValue) isIncreasing = null;
|
||||||
else if (isIncreasing ?? false)
|
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
|
// level was increasing but is now decreasing, so report is unsafe
|
||||||
reportIsSafe = false;
|
reportIsSafe = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else isIncreasing = false;
|
else isIncreasing = false;
|
||||||
differenceInLevels = (previousLevel ?? 0) - currentLevel;
|
differenceInLevels = (previousLevel ?? 0) - currentLevel;
|
||||||
}
|
}
|
||||||
else if (previousLevel.HasValue)
|
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
|
// both currentLevel and previousLevel are equal, so report is unsafe
|
||||||
reportIsSafe = false;
|
reportIsSafe = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Console.WriteLine("{0:s} {1:d}", " differenceInLevels = ", differenceInLevels);
|
Console.WriteLine("{0:s} {1:d}", " differenceInLevels = ", differenceInLevels);
|
||||||
Console.WriteLine("{0:s} {1:d}", " isIncreasing = ", isIncreasing);
|
Console.WriteLine("{0:s} {1:d}", " isIncreasing = ", isIncreasing);
|
||||||
|
@ -161,41 +88,61 @@ namespace Advent24
|
||||||
{
|
{
|
||||||
// difference in levels is outside the safe band, so report is unsafe
|
// difference in levels is outside the safe band, so report is unsafe
|
||||||
if (differenceInLevels < 1 || differenceInLevels > 3)
|
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;
|
reportIsSafe = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// control reaches this line if report has not been proven unsafe yet
|
// control reaches this line if report has not been proven unsafe yet
|
||||||
|
|
||||||
// updating previousPreviousLevel in preparation for the next iteration
|
// updating previousLevel and previousPreviousLevel in preparation for the next iteration
|
||||||
// if problemDampener is still enabled
|
previousPreviousLevel = previousLevel;
|
||||||
if(problemDampenerEnabled) previousPreviousLevel = previousLevel;
|
|
||||||
// updating previousLevel in preparation for the next iteration
|
|
||||||
previousLevel = currentLevel;
|
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', StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
||||||
|
Int32 numberOfSafeReports = 0;
|
||||||
|
|
||||||
|
|
||||||
|
foreach (var report in reportsArray)
|
||||||
|
{
|
||||||
|
if (System.String.IsNullOrEmpty(report)) continue;
|
||||||
|
|
||||||
|
Console.WriteLine("{0:s} {1:d}", "report = ", report);
|
||||||
|
|
||||||
|
var levelsArray = report.Split(' ');
|
||||||
|
|
||||||
|
Int32 numberOfLevels = levelsArray.Length;
|
||||||
|
Boolean reportIsSafe = false;
|
||||||
|
|
||||||
|
// iterate through each index to check subreports
|
||||||
|
// for safety
|
||||||
|
for (Int32 i = 0; i < numberOfLevels; i++)
|
||||||
|
{
|
||||||
|
// check if the current subreport is safe
|
||||||
|
reportIsSafe = ReportIsSafe(levelsArray, i);
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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);
|
Console.WriteLine("{0:s} {1:d}", " reportIsSafe = ", reportIsSafe);
|
||||||
|
|
Loading…
Reference in New Issue