2023-12-09 20:55:02 -06:00
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
namespace MirageMaintenance;
|
2023-12-09 12:17:56 -06:00
|
|
|
|
2023-12-09 21:18:25 -06:00
|
|
|
public class Program
|
2023-12-09 12:17:56 -06:00
|
|
|
{
|
|
|
|
|
public static async Task<int> Main(string[] args)
|
|
|
|
|
{
|
2023-12-09 20:55:02 -06:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 21:15:42 -06:00
|
|
|
var isPart2 = args.Length > 1 && args[1] == "part2";
|
2023-12-09 20:55:02 -06:00
|
|
|
var input = await File.ReadAllLinesAsync(args[0]);
|
|
|
|
|
|
|
|
|
|
var stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
|
|
|
|
|
2023-12-09 21:15:42 -06:00
|
|
|
var result = OasisReport.Parse(input).CalculateSumOfNextValues(isPart2);
|
2023-12-09 20:55:02 -06:00
|
|
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
|
|
|
|
Console.WriteLine($"The sum of all next values is {result}. ({stopwatch.ElapsedMilliseconds}ms)");
|
|
|
|
|
|
|
|
|
|
return (int)result;
|
2023-12-09 12:17:56 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 21:18:25 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a report from the Oasis.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ValueHistories">The value histories.</param>
|
|
|
|
|
/// <returns>An instance of <see cref="OasisReport"/>.</returns>
|
2023-12-09 12:17:56 -06:00
|
|
|
public class OasisReport(
|
|
|
|
|
List<ValueHistory> valueHistories
|
|
|
|
|
)
|
|
|
|
|
{
|
2023-12-09 21:18:25 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the value histories.
|
|
|
|
|
/// </summary>
|
2023-12-09 12:17:56 -06:00
|
|
|
public List<ValueHistory> ValueHistories { get; init; } = valueHistories;
|
|
|
|
|
|
2023-12-09 21:18:25 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// Calculates the sum of all next values.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="backwards">if set to true will extrapolate next values backwards</param>
|
|
|
|
|
/// <returns>The sum of all next values.</returns>
|
2023-12-09 21:15:42 -06:00
|
|
|
public long CalculateSumOfNextValues(bool backwards = false) => ValueHistories
|
|
|
|
|
.Select(valueHistory => valueHistory.CalculateNextValue(backwards))
|
2023-12-09 20:55:02 -06:00
|
|
|
.Sum();
|
|
|
|
|
|
2023-12-09 21:18:25 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// Parses the specified oasis report input.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="oasisReportInput">The oasis report input.</param>
|
|
|
|
|
/// <returns>An instance of <see cref="OasisReport"/>.</returns>
|
2023-12-09 12:17:56 -06:00
|
|
|
public static OasisReport Parse(string[] oasisReportInput)
|
|
|
|
|
{
|
|
|
|
|
var valueHistories = new List<ValueHistory>();
|
|
|
|
|
|
|
|
|
|
foreach (var line in oasisReportInput)
|
|
|
|
|
{
|
|
|
|
|
var values = line
|
|
|
|
|
.Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
|
|
|
|
|
.Select(long.Parse)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var valueHistory = new ValueHistory(values);
|
|
|
|
|
valueHistories.Add(valueHistory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new OasisReport(valueHistories);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 21:18:25 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a value history.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="Values">The values.</param>
|
|
|
|
|
/// <returns>An instance of <see cref="ValueHistory"/>.</returns>
|
2023-12-09 12:17:56 -06:00
|
|
|
public class ValueHistory(
|
|
|
|
|
List<long> values
|
|
|
|
|
)
|
|
|
|
|
{
|
2023-12-09 21:18:25 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the values.
|
|
|
|
|
/// </summary>
|
2023-12-09 12:17:56 -06:00
|
|
|
List<long> Values { get; init; } = values;
|
|
|
|
|
|
2023-12-09 21:18:25 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// Calculates the differences between each value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="values">The values.</param>
|
|
|
|
|
/// <returns>The differences between each value.</returns>
|
2023-12-09 12:17:56 -06:00
|
|
|
public List<long> CalculateDifferences(List<long> values) => Enumerable
|
|
|
|
|
.Range(0, values.Count - 1)
|
|
|
|
|
.Select(i => values[i + 1] - values[i])
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2023-12-09 21:18:25 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// Calculates the history.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The history.</returns>
|
2023-12-09 12:17:56 -06:00
|
|
|
public List<List<long>> CalculateHistory()
|
|
|
|
|
{
|
|
|
|
|
var allHistoricalValues = new List<List<long>>();
|
|
|
|
|
var currentValues = Values;
|
|
|
|
|
|
|
|
|
|
while (
|
|
|
|
|
allHistoricalValues.Count is 0 ||
|
2023-12-09 20:55:02 -06:00
|
|
|
allHistoricalValues.Last().Any(value => value is not 0)
|
2023-12-09 12:17:56 -06:00
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
var historicalValues = CalculateDifferences(currentValues);
|
|
|
|
|
allHistoricalValues.Add(historicalValues);
|
|
|
|
|
currentValues = historicalValues;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return allHistoricalValues;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 21:18:25 -06:00
|
|
|
/// <summary>
|
|
|
|
|
/// Calculates the next value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="backwards">if set to true will extrapolate next value backwards</param>
|
|
|
|
|
/// <returns>The next value.</returns>
|
2023-12-09 21:15:42 -06:00
|
|
|
public long CalculateNextValue(bool backwards = false)
|
2023-12-09 12:17:56 -06:00
|
|
|
{
|
|
|
|
|
var historicalValues = CalculateHistory();
|
2023-12-09 20:55:02 -06:00
|
|
|
var values = historicalValues.Prepend(Values).Reverse().ToList();
|
|
|
|
|
|
2023-12-09 21:15:42 -06:00
|
|
|
if (backwards)
|
|
|
|
|
{
|
|
|
|
|
values.ForEach(value => value.Reverse());
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 20:55:02 -06:00
|
|
|
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];
|
|
|
|
|
|
2023-12-09 21:15:42 -06:00
|
|
|
var extrapolatedValue = backwards
|
|
|
|
|
? currentValues.Last() - previousValues.Last()
|
|
|
|
|
: currentValues.Last() + previousValues.Last();
|
|
|
|
|
|
|
|
|
|
currentValues.Add(extrapolatedValue);
|
2023-12-09 20:55:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return values.Last().Last();
|
2023-12-09 12:17:56 -06:00
|
|
|
}
|
|
|
|
|
}
|