feat: solve part 1...summing to zero is bad check when neg nums are present
This commit is contained in:
@@ -1,10 +1,35 @@
|
||||
namespace MirageMaintenance;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace MirageMaintenance;
|
||||
|
||||
class Program
|
||||
{
|
||||
public static async Task<int> Main(string[] args)
|
||||
{
|
||||
return await Task.FromResult(0);
|
||||
if (args.Length is 0)
|
||||
{
|
||||
Console.WriteLine("Please provide a path to the input file.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (File.Exists(args[0]) is false)
|
||||
{
|
||||
Console.WriteLine("The provided file does not exist.");
|
||||
return -2;
|
||||
}
|
||||
|
||||
var input = await File.ReadAllLinesAsync(args[0]);
|
||||
|
||||
var stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
|
||||
var result = OasisReport.Parse(input).CalculateSumOfNextValues();
|
||||
|
||||
stopwatch.Stop();
|
||||
|
||||
Console.WriteLine($"The sum of all next values is {result}. ({stopwatch.ElapsedMilliseconds}ms)");
|
||||
|
||||
return (int)result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +39,10 @@ public class OasisReport(
|
||||
{
|
||||
public List<ValueHistory> ValueHistories { get; init; } = valueHistories;
|
||||
|
||||
public long CalculateSumOfNextValues() => ValueHistories
|
||||
.Select(valueHistory => valueHistory.CalculateNextValue())
|
||||
.Sum();
|
||||
|
||||
public static OasisReport Parse(string[] oasisReportInput)
|
||||
{
|
||||
var valueHistories = new List<ValueHistory>();
|
||||
@@ -51,7 +80,7 @@ public class ValueHistory(
|
||||
|
||||
while (
|
||||
allHistoricalValues.Count is 0 ||
|
||||
allHistoricalValues.Last().Sum() is not 0
|
||||
allHistoricalValues.Last().Any(value => value is not 0)
|
||||
)
|
||||
{
|
||||
var historicalValues = CalculateDifferences(currentValues);
|
||||
@@ -65,7 +94,23 @@ public class ValueHistory(
|
||||
public long CalculateNextValue()
|
||||
{
|
||||
var historicalValues = CalculateHistory();
|
||||
var values = historicalValues.Prepend(Values).ToList();
|
||||
return 0;
|
||||
var values = historicalValues.Prepend(Values).Reverse().ToList();
|
||||
|
||||
for (var i = 0; i < values.Count; i++)
|
||||
{
|
||||
var currentValues = values[i];
|
||||
|
||||
if (i is 0)
|
||||
{
|
||||
currentValues.Add(0);
|
||||
continue;
|
||||
}
|
||||
|
||||
var previousValues = values[i - 1];
|
||||
|
||||
currentValues.Add(currentValues.Last() + previousValues.Last());
|
||||
}
|
||||
|
||||
return values.Last().Last();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user