HTMLify

LeetCode - Robot Bounded In Circle - Dart
Views: 10 | Author: abh
class Solution {
    bool isRobotBounded(String instructions) {
        List<int> direction = [0, 1], cordinates = [0, 0];
        instructions.runes.forEach((int r) {
                var c = new String.fromCharCode(r);
                if (c == 'G') {
                    cordinates[0] += direction[0];
                    cordinates[1] += direction[1];
                }
                if (c == 'L') {
                    List<int> nd = [0, 0];

                    if (direction[0] == 0 && direction[1] == 1) {
                        nd[0] = -1;
                        nd[1] = 0;
                    }
                    if (direction[0] == 1 && direction[1] == 0) {
                        nd[0] = 0;
                        nd[1] = 1;
                    }
                    if (direction[0] == 0 && direction[1] == -1) {
                        nd[0] = 1;
                        nd[1] = 0;
                    }
                    if (direction[0] == -1 && direction[1] == 0) {
                        nd[0] = 0;
                        nd[1] = -1;
                    }

                    direction[0] = nd[0];
                    direction[1] = nd[1];
                }
                if (c == 'R'){
                    List<int> nd = [0, 0];

                    if (direction[0] == 0 && direction[1] == 1) {
                        nd[0] = 1;
                        nd[1] = 0;
                    }
                    if (direction[0] == 1 && direction[1] == 0) {
                        nd[0] = 0;
                        nd[1] = -1;
                    }
                    if (direction[0] == 0 && direction[1] == -1) {
                        nd[0] = -1;
                        nd[1] = 0;
                    }
                    if (direction[0] == -1 && direction[1] == 0) {
                        nd[0] = 0;
                        nd[1] = 1;
                    }

                    direction[0] = nd[0];
                    direction[1] = nd[1];
                }
        });
        if (((direction[0] != 0 || direction[1] != 1) || (cordinates[0] == 0 && cordinates[1] == 0))) {
            return true;
        }
        return false;
    }
}

Comments