algoalgo-world
algoalgo-world/solve/pledge
solve/pledge

Pledge

pick a language to open the code

TURN_ORDER = (1, 0, 3, 2)
TURN_ANGLE = (1, 0, -1, -2)


def pledge(start, goal, heading):
    cell = start
    facing = heading
    turns = 0
    path = [start]
    while cell != goal:
        if turns == 0 and neighbor(cell, heading) >= 0:
            facing = heading
            cell = neighbor(cell, facing)
            path.append(cell)
            continue
        for turn in range(4):
            way = (facing + TURN_ORDER[turn]) % 4
            other = neighbor(cell, way)
            if other >= 0:
                turns += TURN_ANGLE[turn]
                facing = way
                cell = other
                break
        path.append(cell)
    return path
const TURN_ORDER = [1, 0, 3, 2];
const TURN_ANGLE = [1, 0, -1, -2];

function pledge(start, goal, heading) {
  const path = [start];
  let cell = start;
  let facing = heading;
  let turns = 0;
  while (cell !== goal) {
    if (turns === 0 && neighbor(cell, heading) >= 0) {
      facing = heading;
      cell = neighbor(cell, facing);
      path.push(cell);
      continue;
    }
    for (let turn = 0; turn < 4; turn++) {
      const way = (facing + TURN_ORDER[turn]) % 4;
      const other = neighbor(cell, way);
      if (other < 0) continue;
      turns += TURN_ANGLE[turn];
      facing = way;
      cell = other;
      break;
    }
    path.push(cell);
  }
  return path;
}
static const int TURN_ORDER[4] = {1, 0, 3, 2};
static const int TURN_ANGLE[4] = {1, 0, -1, -2};

int pledge(int start, int goal, int heading, int path[]) {
    int cell = start;
    int facing = heading;
    int turns = 0;
    int count = 0;
    path[count++] = start;
    while (cell != goal) {
        if (turns == 0 && neighbor(cell, heading) >= 0) {
            facing = heading;
            cell = neighbor(cell, facing);
            path[count++] = cell;
            continue;
        }
        for (int i = 0; i < 4; i++) {
            int way = (facing + TURN_ORDER[i]) % 4;
            int other = neighbor(cell, way);
            if (other < 0) continue;
            turns += TURN_ANGLE[i];
            facing = way;
            cell = other;
            break;
        }
        path[count++] = cell;
    }
    return count;
}
constexpr int TURN_ORDER[4] = {1, 0, 3, 2};
constexpr int TURN_ANGLE[4] = {1, 0, -1, -2};

std::vector<int> pledge(int start, int goal, int heading) {
    std::vector<int> path{start};
    int cell = start;
    int facing = heading;
    int turns = 0;
    while (cell != goal) {
        if (turns == 0 && neighbor(cell, heading) >= 0) {
            facing = heading;
            cell = neighbor(cell, facing);
            path.push_back(cell);
            continue;
        }
        for (int turn = 0; turn < 4; turn++) {
            int way = (facing + TURN_ORDER[turn]) % 4;
            int other = neighbor(cell, way);
            if (other < 0) continue;
            turns += TURN_ANGLE[turn];
            facing = way;
            cell = other;
            break;
        }
        path.push_back(cell);
    }
    return path;
}
static readonly int[] TurnOrder = { 1, 0, 3, 2 };
static readonly int[] TurnAngle = { 1, 0, -1, -2 };

static List<int> Pledge(int start, int goal, int heading) {
    var path = new List<int> { start };
    int cell = start;
    int facing = heading;
    int turns = 0;
    while (cell != goal) {
        if (turns == 0 && Neighbor(cell, heading) >= 0) {
            facing = heading;
            cell = Neighbor(cell, facing);
            path.Add(cell);
            continue;
        }
        for (int turn = 0; turn < 4; turn++) {
            int way = (facing + TurnOrder[turn]) % 4;
            int other = Neighbor(cell, way);
            if (other < 0) continue;
            turns += TurnAngle[turn];
            facing = way;
            cell = other;
            break;
        }
        path.Add(cell);
    }
    return path;
}
static final int[] TURN_ORDER = {1, 0, 3, 2};
static final int[] TURN_ANGLE = {1, 0, -1, -2};

static List<Integer> pledge(int start, int goal, int heading) {
    List<Integer> path = new ArrayList<>();
    path.add(start);
    int cell = start;
    int facing = heading;
    int turns = 0;
    while (cell != goal) {
        if (turns == 0 && neighbor(cell, heading) >= 0) {
            facing = heading;
            cell = neighbor(cell, facing);
            path.add(cell);
            continue;
        }
        for (int turn = 0; turn < 4; turn++) {
            int way = (facing + TURN_ORDER[turn]) % 4;
            int other = neighbor(cell, way);
            if (other < 0) continue;
            turns += TURN_ANGLE[turn];
            facing = way;
            cell = other;
            break;
        }
        path.add(cell);
    }
    return path;
}
watch it run, then read it