Another day, another challenge. Today, your task is to help your grandmother visit her friends.
This challenge comes from user @g964 on CodeWars:
Your granny, who lives in town X0, has friends. These friends are given in an array, for example: array of friends is
[ "A1", "A2", "A3", "A4", "A5" ].
Note: the order of friends is this array and it must not be changed since this order gives the order in which they will be visited.
These friends inhabit towns and you have an array with friends and towns, for example:
[ ["A1", "X1"], ["A2", "X2"], ["A3", "X3"], ["A4", "X4"] ]
or
[ ("A1", "X1"), ("A2", "X2"), ("A3", "X3"), ("A4", "X4") ]
or
(C)
{"A1", "X1", "A2", "X2", "A3", "X3", "A4", "X4"}which means A1 is in town X1, A2 in town X2... It can happen that we don't know the town of one of the friends.
Your granny wants to visit her friends and to know how many miles she will have to travel. Can you help her figure it out?
Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (16)
I think that some of the details are missing here...
What are the distances?
I was able to find codewars.com/kata/help-your-granny... which has additional information.
Ya agreed...
Feels like we are missing some kind of map, or distances between different towns
I'm making the following assumptions, but I think I have a solution:
friendTownList
uses format C (flattened list)Could be one (ugly) line if not counting error handling in the case where we don't know where a friend lives, could also be smaller if
friendTownList
was a hash to start with~I'll update / post another solution if the question is updated with more information!
Wow! Love that we are all over here just complaining about not having enough data and you just went for it!
Love it! Keep up the awesome work on these challenges!
Erlang:
note: I followed the link to the codewars page and am using their list of distances. I also assume Granny is coming home after visiting her friends. A different list of distances can be passed in, without limit on length. But if the correlation between friend1 is in town1 breaks down then I'd need to add a map/hashtable for that lookup.
devto6:find_distance([100, 200, 250, 300]).
889.0363202746577
//TS
//TDD
TEST
this is my solution in PHP, although the description is quite ambiguous...
My solution in Swift, first time I use nested functions :
JavaScript
It doesn't use a smart algorithm to calculate the distance: grandma will visit a friend and then go back home, and continue with the next friend. If she doesn't know the city where a friend lives, she'll stay at home and meet the next friend.
Another version using array methods (and kind of unreadable):
Here is a demo on CodePen.
This is a bit confusing. If you don't know the next city to visit, how will you calculate the distance between the two cities?
The problem says that the cities must be visited in the order in which they appear in the array, but yes, details are missing, we don't know the distance between the cities, which is necessary information to solve the problem.
It's even worse: "It can happen that we don't know the town of one of the friends." Even if we had the distances, we may not even know which is our next destination.
Take this comment as a commitment to do this challenge sometime over the holiday weekend, pinky promise!
Here I am to fulfill my promise!
I followed the directions at: codewars.com/kata/help-your-granny...
Buttt... Even then they left something to be desired lol
But I made do! I made a few assumptions, changes to the rules.
The one rule I ignored (because honestly I didn't read it till later) is:
I'm gonna justify it by saying that this rule means that the map changes depending on where Granny goes. I didn't think granny's plan should alter the map, but also I kinda just didn't read that rule!
I also created the rule that there are only roads between adjacent towns, and there is a road from each down to your grannies
Home
town. This makes the map a lot like the wheel and spoke map that is drawn in the linked problem.These two rules combine to mean that not every map is valid, so I return an Error when we encounter an invalid map.
This change of rules, actually led to some more complexity lol. Now it wasn't straight forward what the best path from any arbitrary town to another was. Maybe you should travel "town to town" and maybe it was better to go home in the middle.
I take whichever of these two paths turns out to be shorter!
It's close to 200 lines, so instead of posting it here I'm just gonna post a link to the Github!
github.com/coreyja/dev-to-challeng...