halcyon92's blog

By halcyon92, history, 5 years ago, In English

I'm trying to do the Google Kick Start problems in practice mode. I'm failing to figure out what went wrong with my code. Can someone help me figure this one out?

Robot Path Decoding

import java.util.Scanner;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int t = input.nextInt();

        for(int u=1; u <= t; u++) {

            String pgm = input.next();

            Point p = new Point(1, 1);

            Stack<Integer> digits = new Stack<Integer>();
            Stack<Long> multiplier = new Stack<Long>();

            multiplier.push(1L);
            for(int i=0; i < pgm.length(); i++) {
                Character ch = pgm.charAt(i);

                if (Character.isDigit(ch)) {
                    digits.push(Integer.parseInt(ch+""));
                    continue;
                }

                long curMultiplier = multiplier.peek();

                if (ch == 'S') {
                    p = p.moveSouth(curMultiplier);
                } else if (ch == 'N') {
                    p = p.moveNorth(curMultiplier);
                } else if (ch == 'E') {
                    p = p.moveEast(curMultiplier);
                } else if (ch == 'W') {
                    p = p.moveWest(curMultiplier);
                } else {
                    if (ch == '(') {
                        if (!digits.empty()) {
                            curMultiplier = curMultiplier * digits.pop();
                            multiplier.push(curMultiplier);
                        }
                    } else {
                        //ch == ')'
                        multiplier.pop();
                    }
                }
            }
            System.out.println("Case #" + (u) + ": " + p.x + " " + p.y);
        }
    }

    static class Point {

        public long x;
        public long y;

        public static final int EDGE = 1000 * 1000 * 1000;

        public Point(long x, long y) {
            this.x = x;
            this.y = y;
        }

        public Point moveNorth(long turns) {
            this.y = (this.y - turns) > 0 ? (this.y - turns): (this.y + EDGE - turns);

            return this;
        }

        public Point moveWest(long turns) {
            this.x = (this.x - turns) > 0 ? (this.x - turns): (this.x + EDGE - turns);

            return this;
        }

        public Point moveEast(long turns) {
            long cur = this.x + turns;

            this.x = cur % EDGE;

            return this;
        }

        public Point moveSouth(long turns) {
            long cur = this.y + turns;

            this.y = cur % EDGE;

            return this;
        }
    }
}
  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
  Vote: I like it +2 Vote: I do not like it

Two of the possible mistakes that I could find are 1. Your curMultiplier could overflow as you are not taking it modulo 2. In moveNorth and moveWest your this.x-turns can be less than negative of EDGE so it's better to also take modulo here.