I am trying to create a code that will solve the CCC 2018 J3 Problem "Are We there Yet?".
There are 5 cities, and would like to calculate the distance per city.
Expected output:
enter image description here
Link: https://cemc.math.uwaterloo.ca/contests/computing/past_ccc_contests/2018/stage%201/juniorEF.pdf
My code is this:
/// GET/api/J3/DistanceCalculator/{distance1}/{distance2}/{distance3}/{distance4}
/// api/J3/DistanceCalculator/3/10/12/5
/// Output:
/// First Line: 0/3/13/25/30 -> 0 is the distance of the current destination
/// Second Line: 3/0/10/22/27
/// Third Line: 13/10/0/12/17
/// Fourth Line: 25/22/12/0/5
/// Fifth Line: 30/27/17/5/0
/// </example>
// GET api/J3/DistanceCalculator/{distance1}/{distance2}/{distance3}/{distance4}/
[HttpGet]
[Route("api/J3/DistanceCalculator/{distance1}/{distance2}/{distance3}/{distance4}")]
public int DistanceCalculator(int distance1, int distance2, int distance3, int distance4)
{
//declare variables and array
List <int> cities = new List<int> {5};
List <int> distances = new List<int> {4};
int currentDistance = 0;
//loop representing 5 different starting points of 5 cities
for (int i = 0; i < 5; i++)
{
//write distance[i]
//Debug.WriteLine("You are starting in city: " + i);
//start at 0 since city 1 is starting point
int cities[0] = 0;
//continue adding distance per number given
int cities[i] = cities[i - 1] + distances[i-1];
return cities[i+1] + " " + cities[i+2] + " " + cities[i+3] + " " + cities[i+4] + " " + cities[i+5];
//Debug.WriteLine("East-------------");
//Debug.WriteLine("You are starting in city: " + i + " and driving to city 0");distances[
int distances[i] = cities[i + 1] - cities[i];
//create a loop calculating the distance of the city from the original destination
for (int j = i; j >= 0; j--)
{
int formerDistance = cities[j] - cities[j - 1];
//Debug.WriteLine("The distance from previous city is " + j);
//convert negative numbers into positive
if (formerDistance < 0)
{
formerDistance *= -1;
}
else
{
formerDistance *= 1;
}
//print
return formerDistance;
}
//show former distance
return formerDistances;
//Debug.WriteLine("West--------------");
//Debug.WriteLine("You are started in city: " + i + " and driving to city 4");
//calculates the city from the starting point
for (int j = i; j < 5; j++)
{
distances[j + 1] = distances[j] + distances[j + 1];
int city5 = 0;
int city4 = distances[j-1] - city5;
int city3 = distances[j - 1] - city4;
int city2 = distances[j - 1] - city5;
int city1 = distances[j - 1] - city2;
//Debug.WriteLine("The distance from previous city is " + i);
//Debug.WriteLine("You are starting in city: " + j);
}
}
currentDistance = distance1 + distance2 + distance3 + distance4;
//return the distance
return currentDistance;
}
}
2
Answers
Let’s split the initial problem into smaller ones:
Let me use console to solve the problem; you can adapt it for REST, WinForms etc.
Solve for one line (let it be line #
index
):Solve for the entire table:
Output
Finally, let’s combine it all together
I think it’s good to apply the permutations problem from algebra lesson.
Hence:
distance[i+1] += distance[i];
and then with extracting the solution from city3-city2 for example, use
(distance[j] – distance[i]);
to get distance 2.