Partial day2
parent
d3ec82e9f8
commit
95702c7a3e
|
@ -0,0 +1,212 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Advent24
|
||||||
|
{
|
||||||
|
internal class Day2
|
||||||
|
{
|
||||||
|
public Day2()
|
||||||
|
{
|
||||||
|
string fileData = System.IO.File.ReadAllText(@"..\..\..\inputd2.txt");
|
||||||
|
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? previousPreviousLevel = null;
|
||||||
|
Int32? discardedLevel = null;
|
||||||
|
Int32 differenceInLevels = 0;
|
||||||
|
Boolean? isIncreasing = null;
|
||||||
|
Boolean reportIsSafe = true;
|
||||||
|
Boolean problemDampenerEnabled = true;
|
||||||
|
|
||||||
|
foreach (var level in levelsArray)
|
||||||
|
{
|
||||||
|
Console.WriteLine("{0:s} {1:d}", " level = ", level);
|
||||||
|
|
||||||
|
currentLevel = System.Convert.ToInt32(level);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("{0:s} {1:d}", " reportIsSafe = ", reportIsSafe);
|
||||||
|
// if report has not been proven unsafe, then it must be safe
|
||||||
|
if (reportIsSafe) numberOfSafeReports++;
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("{0:s} {1:d}", "numberOfSafeReports = ", numberOfSafeReports);
|
||||||
|
|
||||||
|
// to make sure the console window doesn't disappear
|
||||||
|
Console.ReadLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,7 +7,8 @@ namespace Advent24
|
||||||
{
|
{
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
_ = new Day1();
|
//_ = new Day1();
|
||||||
|
_ = new Day2();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue