feat: start working on part 1 solution
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
namespace MirageMaintenance.Tests;
|
||||
|
||||
public class UnitTest1
|
||||
{
|
||||
[Theory]
|
||||
[MemberData(nameof(TestData.OasisReportParseTestData), MemberType = typeof(TestData))]
|
||||
public void Parse_GivenAReportInput_ItShouldReturnExpectedOasisReportInstance(string[] input, OasisReport expected)
|
||||
{
|
||||
OasisReport.Parse(input).Should().BeEquivalentTo(expected);
|
||||
}
|
||||
|
||||
public static class TestData
|
||||
{
|
||||
public static IEnumerable<object[]> OasisReportParseTestData =>
|
||||
new List<object[]>
|
||||
{
|
||||
new object[]
|
||||
{
|
||||
new string[]
|
||||
{
|
||||
"0 3 6 9 12 15",
|
||||
"1 3 6 10 15 21",
|
||||
"10 13 16 21 30 45",
|
||||
},
|
||||
new OasisReport(
|
||||
[
|
||||
new([0, 3, 6, 9, 12, 15]),
|
||||
new([1, 3, 6, 10, 15, 21]),
|
||||
new([10, 13, 16, 21, 30, 45]),
|
||||
]
|
||||
)
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace MirageMaintenance.Tests;
|
||||
|
||||
public class UnitTest1
|
||||
{
|
||||
[Fact]
|
||||
public void Test1()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
namespace MirageMaintenance.Tests;
|
||||
|
||||
public class ValueHistoryTests
|
||||
{
|
||||
[Theory]
|
||||
[MemberData(nameof(TestData.CalculateDifferencesTestData), MemberType = typeof(TestData))]
|
||||
public void CalculateDifferences_GivenAListOfValues_ItShouldReturnAListOfDifferences(List<long> values, List<long> expected)
|
||||
{
|
||||
new ValueHistory([]).CalculateDifferences(values).Should().BeEquivalentTo(expected);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(TestData.CalculateHistoryTest), MemberType = typeof(TestData))]
|
||||
public void CalculateHistory_GivenAListOfValues_ItShouldReturnAListOfHistoricalValues(List<long> values, List<List<long>> expected)
|
||||
{
|
||||
new ValueHistory(values).CalculateHistory().Should().BeEquivalentTo(expected);
|
||||
}
|
||||
|
||||
public static class TestData
|
||||
{
|
||||
public static IEnumerable<object[]> CalculateHistoryTest =>
|
||||
new List<object[]>
|
||||
{
|
||||
new object[]
|
||||
{
|
||||
new List<long> { 0, 3, 6, 9, 12, 15 },
|
||||
new List<List<long>>
|
||||
{
|
||||
new() { 3, 3, 3, 3, 3 },
|
||||
new() { 0, 0, 0, 0 },
|
||||
},
|
||||
},
|
||||
new object[]
|
||||
{
|
||||
new List<long> { 1, 3, 6, 10, 15, 21 },
|
||||
new List<List<long>>
|
||||
{
|
||||
new() { 2, 3, 4, 5, 6 },
|
||||
new() { 1, 1, 1, 1 },
|
||||
new() { 0, 0, 0 },
|
||||
},
|
||||
},
|
||||
new object[]
|
||||
{
|
||||
new List<long> { 10, 13, 16, 21, 30, 45 },
|
||||
new List<List<long>>
|
||||
{
|
||||
new() { 3, 3, 5, 9, 15 },
|
||||
new() { 0, 2, 4, 6 },
|
||||
new() { 2, 2, 2 },
|
||||
new() { 0, 0 },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
public static IEnumerable<object[]> CalculateDifferencesTestData =>
|
||||
new List<object[]>
|
||||
{
|
||||
new object[]
|
||||
{
|
||||
new List<long> { 0, 3, 6, 9, 12, 15 },
|
||||
new List<long> { 3, 3, 3, 3, 3 },
|
||||
},
|
||||
new object[]
|
||||
{
|
||||
new List<long> { 1, 3, 6, 10, 15, 21 },
|
||||
new List<long> { 2, 3, 4, 5, 6 },
|
||||
},
|
||||
new object[]
|
||||
{
|
||||
new List<long> { 10, 13, 16, 21, 30, 45 },
|
||||
new List<long> { 3, 3, 5, 9, 15 },
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,71 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
Console.WriteLine("Hello, World!");
|
||||
namespace MirageMaintenance;
|
||||
|
||||
class Program
|
||||
{
|
||||
public static async Task<int> Main(string[] args)
|
||||
{
|
||||
return await Task.FromResult(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class OasisReport(
|
||||
List<ValueHistory> valueHistories
|
||||
)
|
||||
{
|
||||
public List<ValueHistory> ValueHistories { get; init; } = valueHistories;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public class ValueHistory(
|
||||
List<long> values
|
||||
)
|
||||
{
|
||||
List<long> Values { get; init; } = values;
|
||||
|
||||
public List<long> CalculateDifferences(List<long> values) => Enumerable
|
||||
.Range(0, values.Count - 1)
|
||||
.Select(i => values[i + 1] - values[i])
|
||||
.ToList();
|
||||
|
||||
public List<List<long>> CalculateHistory()
|
||||
{
|
||||
var allHistoricalValues = new List<List<long>>();
|
||||
var currentValues = Values;
|
||||
|
||||
while (
|
||||
allHistoricalValues.Count is 0 ||
|
||||
allHistoricalValues.Last().Sum() is not 0
|
||||
)
|
||||
{
|
||||
var historicalValues = CalculateDifferences(currentValues);
|
||||
allHistoricalValues.Add(historicalValues);
|
||||
currentValues = historicalValues;
|
||||
}
|
||||
|
||||
return allHistoricalValues;
|
||||
}
|
||||
|
||||
public long CalculateNextValue()
|
||||
{
|
||||
var historicalValues = CalculateHistory();
|
||||
var values = historicalValues.Prepend(Values).ToList();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user