#include <stdio.h>
#define MAXMAZEHEIGHT 20
#define MAXMAZEWIDTH  20
#define MAXANSWER     20

typedef enum hextype {
  unknown,
  vines,
  visited
} hextype;

typedef struct position {
  int row;
  int col;
} position;

/* responses */
#define OUCH	'n'
#define OKAY	'y'
#define FREEDOM	'E'

static hextype maze[2*MAXMAZEHEIGHT-1][2*MAXMAZEWIDTH-1];

static void search(position pos, int dir);
static int getnewdir(int lastdir, int dirind);
static position dirnewpos(position pos, int dir);
static int checkresponse(char *response);
static int getbackdir(int lastdir);

main() {
  int ii, jj;
  position pos = {MAXMAZEHEIGHT-1, MAXMAZEWIDTH-1};

  for (ii = 0; ii < 2*MAXMAZEHEIGHT-1; ii++) {
    for (jj = 0; jj < 2*MAXMAZEWIDTH-1; jj++) {
      maze[ii][jj] = unknown;
    }
  }
  search(pos, 0);
  fprintf(stderr, "No exit!\n");
  exit(1);
}

static void search(position pos, int lastdir) {
  int dirind;
  int newdir;
  position newpos;
  char response[MAXANSWER];

  maze[pos.row][pos.col] = visited;
  for (dirind = 0; dirind < 6; dirind++) {
    newdir = getnewdir(lastdir, dirind);
    newpos = dirnewpos(pos, newdir);
    if (maze[newpos.row][newpos.col] == unknown) {
      printf("%d\n", newdir); fflush(stdout);
      fgets(response, sizeof(response), stdin);
      switch (checkresponse(response)) {
      case OUCH:
	maze[newpos.row][newpos.col] = vines;
	break;
      case OKAY:
	search(newpos, newdir);
	break;
      case FREEDOM:
	exit(0);
	break;
      }
    }
  }
  if (lastdir > 0) {
    /* we came from somewhere else, so go back */
    printf("%d\n", getbackdir(lastdir)); fflush(stdout);
    fgets(response, sizeof(response), stdin);
    if (checkresponse(response) != OKAY) {
      fprintf(stderr, "could not back up!\n");
      exit(1);
    }
  }
}
static int getnewdir(int lastdir, int dirind) {
  /* try to go as straight as possible first */
  /* since % is not "mod" and since dir is 1 to 6 instead of 0 to 5,
     avoid negative values by adding (6 - 1) to offsets */
  static int newdir[] = {5, 6, 4, 7, 3, 2};
  return (lastdir + newdir[dirind]) % 6 + 1;
}
static position dirnewpos(position pos, int dir) {
  static position offset[6][2] = {
    /*       even col,  odd col */
    /* dir,  row, col  row, col */
    /* 1 */ {{-1,  0}, {-1,  0}},
    /* 2 */ {{ 0,  1}, {-1,  1}},
    /* 3 */ {{ 1,  1}, { 0,  1}},
    /* 4 */ {{ 1,  0}, { 1,  0}},
    /* 5 */ {{ 1, -1}, { 0, -1}},
    /* 6 */ {{ 0, -1}, {-1, -1}}
  };
  position newpos;

  newpos.row = pos.row + offset[dir-1][pos.col%2].row;
  newpos.col = pos.col + offset[dir-1][pos.col%2].col;
  return newpos;
}
static int checkresponse(char *response) {
  /* responses are distinguished by their first letter */
  return response[0];
}
static int getbackdir(int lastdir) {
  return (lastdir + 2) % 6 + 1;
}
