How to Take a Ten Minute Walk in C


The challenge

You live in the city of Cartesia where all roads are laid out in a perfect grid. You arrived ten minutes too early to an appointment, so you decided to take the opportunity to go for a short walk. The city provides its citizens with a Walk Generating App on their phones — everytime you press the button it sends you an array of one-letter strings representing directions to walk (eg. [‘n’, ‘s’, ‘w’, ‘e’]). You always walk only a single block for each letter (direction) and you know it takes you one minute to traverse one city block, so create a function that will return true if the walk the app gives you will take you exactly ten minutes (you don’t want to be early or late!) and will, of course, return you to your starting point. Return false otherwise.

Note: you will always receive a valid array containing a random assortment of direction letters (‘n', ‘s', ‘e', or ‘w' only). It will never give you an empty array (that's not a walk, that's standing still!).

The solution in C

Option 1:

#include <stdbool.h>

bool isValidWalk(const char *walk) {
  if (strlen(walk) != 10) return 0;
  
  int h = 0, v = 0;
  while(*walk) {
    switch(*walk) {
      case 'n': ++v; break;
      case 's': --v; break;
      case 'e': ++h; break;
      case 'w': --h; break;
    }
    ++walk;
  }
  return h == 0 && v == 0;
}

Option 2:

#include <stdbool.h>

bool isValidWalk(const char *walk) {
     int i=0, north_south=0, vest_est=0; 
     for(; i< walk[i]; i++){
       if(walk[i] == 'n' ) north_south++;
       if(walk[i] == 's' ) north_south--;
       if(walk[i] == 'e' ) vest_est++;
       if(walk[i] == 'w' ) vest_est--;
        
     }
     if(i==10 && north_south==0 && vest_est==0) return true; else return false;
}

Option 3:

#include <stdbool.h>

bool isValidWalk(const char *walk) {

   char* c = walk;
   int up, right, count;
   up = right = count = 0;
   while( *c !='\0' ){
     switch(*c){
       case 'n':
         count++;
         up++;
         break;
       case 's':
         count++;
         up--;
         break;
       case 'e':
         count++;
         right++;
         break;
       case 'w':
         count++;
         right--;
         break;
     
     }
     c++;    
   }
   return up == 0 && right == 0 && count == 10;
}

Test cases to validate our solution

#include <criterion/criterion.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>

void tester(char *twalk, bool expected);

Test(Fixed_Tests, should_return_true_for_a_valid_walk) {
  tester("nsnsnsnsns", true);
  tester("ewewnsnsew", true);
  tester("nsewnsewns", true);
  tester("sewnnsewns", true);
}

Test(Fixed_Tests, should_return_false_if_walk_is_too_short) {
  tester("n", false);
  tester("ns", false);
}

Test(Fixed_Tests, should_return_false_if_walk_is_too_long) {
  tester("nsnsnsnsnsns", false);
  tester("nsewnsewnsewnsew", false);
  tester("nsnsnsnsnsewewewewew", false);
}

Test(Fixed_Tests, should_return_false_if_walk_does_not_bring_you_back_to_start) {
  tester("nsnsnsnsnn", false);
  tester("eeewnsnsew", false);
}

static bool solution(const char *swalk) {
    int i=0, n=0, e=0, w=0, s=0;
    while(*swalk) {
        char current = *swalk++;
        if(current == 'n') n++; else
        if(current == 'e') e++; else
        if(current == 'w') w++; else
        if(current == 's') s++; i++;
    }
    return i == 10 && n == s && w == e;
}

static char* getWalk(size_t steps) {
    char *gwalk = (char*) malloc((steps + 1) * sizeof(char));
    if(steps == 10) {
        const char* valid[6] = {"nnnnnsssss", "nnnnewssss", "nnneewwsss",
                                "nneeewwwss", "neeeewwwws", "eeeeewwwww"};
        const char news[4] = {'n', 'e', 'w', 's'};
        strcpy(gwalk, valid[rand() % 6]);
        if(rand() % 2) {
            gwalk[rand() % 10] = news[rand() % 4];
        }
    }
    else {
        size_t i = 0;
        char n_e[2] = {'n', 'e'};
        char w_s[2] = {'w', 's'};
        for(; i<steps; i++) {
            if(rand() % 2) {
                gwalk[i] = n_e[rand() % 2];
            }
            else {
                gwalk[i] = w_s[rand() % 2];
            }
        }
        gwalk[i] = '\0';
    }
    return gwalk;
}

Test(Random_Tests, should_pass_all_the_tests_provided) {
    srand(time(NULL));

    int count = 100;
    while(count--) {
        int minutes[25] = {10,10,10,8,10,10,10,1,10,10,10,2,10,
                           10,10,9,10,10,10,11,10,10,10,100,10};
        int rand_mins = minutes[rand() % 25];
        char *rand_walk = getWalk(rand_mins);
        int answer = solution(rand_walk);
        tester(rand_walk, answer);
        free(rand_walk); rand_walk = NULL;
    }
}