top | item 17762236

(no title)

morazow | 7 years ago

How would you rate this solution? Assuming we have static keypad grid.

  long moves(int start = 1, int len) {
    assert(len > 0);
    long[] cur = new long[10];
    for (int i = 0; i < 10; i++) {
      cur[i] = 1;
    }
  
    long[] nxt = new long[10];
    while (len-- > 0) {
      nxt[0] = cur[4] + cur[6];
      nxt[1] = cur[8] + cur[6];
      nxt[2] = cur[7] + cur[9];
      nxt[3] = cur[4] + cur[8];
      nxt[4] = cur[3] + cur[9] + cur[0];
      nxt[5] = 0;
      nxt[6] = cur[1] + cur[7] + cur[0];
      nxt[7] = cur[2] + cur[6];
      nxt[8] = cur[1] + cur[3];
      nxt[9] = cur[4] + cur[2];

      for (int i = 0; i < 10; i++) {
        cur[i] = nxt[i];
      }
    }
  
    return cur[start];
  }

discuss

order

bjourne|7 years ago

Very good! However there is an off-by-one error as the exercise asks for paths of lengths not number of nodes. If you came up with that in 45 minutes without googling, you are very strong indeed.