using System.Collections.Generic;
using System.Threading.Tasks;
namespace CatchmentCalculator
static void Main(string[] args)
Console.WriteLine("Running tests for CatchmentCalculator");
TestNewStationCatchment();
TestExistingStationCatchment();
Console.WriteLine("Press a key to continue");
public static List<int> GetStationCatchment(double[,] times, List<int> stationNodes, int targetStationNode){
List<int> catchment = new List<int>();
List<int> stationNodesIncludingTarget = stationNodes;
if(!(stationNodesIncludingTarget.Contains(targetStationNode))){
stationNodesIncludingTarget.Add(targetStationNode);
for (int i=0;i<times.GetLength(0);i++){
double minTime = double.MaxValue;
for (int j=0;j<stationNodesIncludingTarget.Count;j++){
currentTime = times[i, stationNodesIncludingTarget[j] ];
closestNode = stationNodesIncludingTarget[j];
if(closestNode==targetStationNode){
public static void TestNewStationCatchment()
var times = TestHelperMethods.GetSquareGridTimes(5);
var stationNodes = new List<int> { 0, 12 };
var targetStationNode = 8;
var expectedCatchment = new List<int> { 2, 3, 4, 8, 9, 14 };
var catchment = GetStationCatchment(times, stationNodes, targetStationNode);
TestHelperMethods.AssertAndOutputResult("GetCatchmentForNewStation", expectedCatchment, catchment);
public static void TestExistingStationCatchmentReordered()
var times = TestHelperMethods.GetSquareGridTimes(5);
var stations = new List<int> { 12, 8, 0 };
var targetStationNode = 0;
var expectedCatchment = new List<int> { 0, 1, 5, 6, 10 };
var catchment = GetStationCatchment(times, stations, targetStationNode);
TestHelperMethods.AssertAndOutputResult("GetCatchmentForExistingStationReordered", expectedCatchment, catchment);
public static void TestExistingStationCatchment()
var times = TestHelperMethods.GetSquareGridTimes(5);
var stations = new List<int> { 0, 8, 12 };
var targetStationNode = 0;
var expectedCatchment = new List<int> { 0, 1, 5, 6, 10 };
var catchment = GetStationCatchment(times, stations, targetStationNode);
TestHelperMethods.AssertAndOutputResult("GetCatchmentForExistingStation", expectedCatchment, catchment);
public static class TestHelperMethods
public static void AssertAndOutputResult(string test, List<int> expected, List<int> actual)
var success = AreEquivalent(expected, actual);
Console.WriteLine($"{test}: PASS");
Console.WriteLine($"{test}: FAIL");
Console.WriteLine($"Expected: [{string.Join(",", expected)}]");
Console.WriteLine($"Actual: [{string.Join(",", actual)}]");
public static double[,] GetSquareGridTimes(int width)
var nodeCount = width * width;
var times = new double[nodeCount, nodeCount];
for (int i = 0; i < nodeCount; i++)
for (int j = 0; j < nodeCount; j++)
times[i, j] = Math.Sqrt(Math.Pow(xi - xj, 2) + Math.Pow(yi - yj, 2));
public static bool AreEquivalent(List<int> x, List<int> y)
return !(x.Except(y).Any() || y.Except(x).Any());