feat: solve part 2
This commit is contained in:
@@ -23,8 +23,25 @@ public class ValueHistoryTests
|
|||||||
new ValueHistory(values).CalculateNextValue().Should().Be(expected);
|
new ValueHistory(values).CalculateNextValue().Should().Be(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(TestData.CalculateNextValueTestDataBackwards), MemberType = typeof(TestData))]
|
||||||
|
public void CalculateNextValue_GivenAListOfValuesAndBackwardsIsTrue_ItShouldReturnTheNextValue(List<long> values, long expected)
|
||||||
|
{
|
||||||
|
new ValueHistory(values).CalculateNextValue(true).Should().Be(expected);
|
||||||
|
}
|
||||||
|
|
||||||
public static class TestData
|
public static class TestData
|
||||||
{
|
{
|
||||||
|
public static IEnumerable<object[]> CalculateNextValueTestDataBackwards =>
|
||||||
|
new List<object[]>
|
||||||
|
{
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new List<long> { 10, 13, 16, 21, 30, 45 },
|
||||||
|
5,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
public static IEnumerable<object[]> CalculateNextValueTestData =>
|
public static IEnumerable<object[]> CalculateNextValueTestData =>
|
||||||
new List<object[]>
|
new List<object[]>
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -18,12 +18,13 @@ class Program
|
|||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var isPart2 = args.Length > 1 && args[1] == "part2";
|
||||||
var input = await File.ReadAllLinesAsync(args[0]);
|
var input = await File.ReadAllLinesAsync(args[0]);
|
||||||
|
|
||||||
var stopwatch = new Stopwatch();
|
var stopwatch = new Stopwatch();
|
||||||
stopwatch.Start();
|
stopwatch.Start();
|
||||||
|
|
||||||
var result = OasisReport.Parse(input).CalculateSumOfNextValues();
|
var result = OasisReport.Parse(input).CalculateSumOfNextValues(isPart2);
|
||||||
|
|
||||||
stopwatch.Stop();
|
stopwatch.Stop();
|
||||||
|
|
||||||
@@ -39,8 +40,8 @@ public class OasisReport(
|
|||||||
{
|
{
|
||||||
public List<ValueHistory> ValueHistories { get; init; } = valueHistories;
|
public List<ValueHistory> ValueHistories { get; init; } = valueHistories;
|
||||||
|
|
||||||
public long CalculateSumOfNextValues() => ValueHistories
|
public long CalculateSumOfNextValues(bool backwards = false) => ValueHistories
|
||||||
.Select(valueHistory => valueHistory.CalculateNextValue())
|
.Select(valueHistory => valueHistory.CalculateNextValue(backwards))
|
||||||
.Sum();
|
.Sum();
|
||||||
|
|
||||||
public static OasisReport Parse(string[] oasisReportInput)
|
public static OasisReport Parse(string[] oasisReportInput)
|
||||||
@@ -91,11 +92,16 @@ public class ValueHistory(
|
|||||||
return allHistoricalValues;
|
return allHistoricalValues;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long CalculateNextValue()
|
public long CalculateNextValue(bool backwards = false)
|
||||||
{
|
{
|
||||||
var historicalValues = CalculateHistory();
|
var historicalValues = CalculateHistory();
|
||||||
var values = historicalValues.Prepend(Values).Reverse().ToList();
|
var values = historicalValues.Prepend(Values).Reverse().ToList();
|
||||||
|
|
||||||
|
if (backwards)
|
||||||
|
{
|
||||||
|
values.ForEach(value => value.Reverse());
|
||||||
|
}
|
||||||
|
|
||||||
for (var i = 0; i < values.Count; i++)
|
for (var i = 0; i < values.Count; i++)
|
||||||
{
|
{
|
||||||
var currentValues = values[i];
|
var currentValues = values[i];
|
||||||
@@ -108,7 +114,11 @@ public class ValueHistory(
|
|||||||
|
|
||||||
var previousValues = values[i - 1];
|
var previousValues = values[i - 1];
|
||||||
|
|
||||||
currentValues.Add(currentValues.Last() + previousValues.Last());
|
var extrapolatedValue = backwards
|
||||||
|
? currentValues.Last() - previousValues.Last()
|
||||||
|
: currentValues.Last() + previousValues.Last();
|
||||||
|
|
||||||
|
currentValues.Add(extrapolatedValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
return values.Last().Last();
|
return values.Last().Last();
|
||||||
|
|||||||
Reference in New Issue
Block a user