This one is just walking on an infinite path. The description of the solution seems simpler than the actual solution. Though while fooling around, my first attempt was almost a one-liner and it just worked first try...
int dist = 0;
foreach (var c in program)
{
while (path[++dist % 6] != c) ;
}
return dist;
For part 2... the description seems a bit easy. The shortest distance would obviously just be to do all rs, then vs, and so on, and it would be a pretty easy count of those. But a quick look at my actual input, it lacks any os, which would throw things off! I'm assuming other people have different missing letters so I wouldn't want to just hard-code that missing letter. Just need to figure out the optimal path for the letters I do have.
var curPath = path.Rotate(0);
while (curPath[0] != current)
{
curPath = curPath.Rotate(1);
}
curPath = curPath.Rotate(1);
for (int i = 6; i > 0; i--)
{
if (waypoints[curPath[i - 1]] > 0)
{
return (curPath[i - 1], i);
}
}
This is probably not the cleanest way to do it, but honestly I just couldn't really see a "clean" way to do it. But this works, as annoying as it is. Probably some tricky thing with modulos?
That Rotate() function is a simple string extension I wrote that "rotates" a string such as roygbv by the given number of characters; so calling "roygbv".Rotate(2) will return ygbvro. Helpful in this case.
public static string Rotate(this string str, int n)
{
return str.Substring(n) + str.Substring(0, n);
}