Han

Han Ferik

Home | Projects | AP CSA

Secret Instructions

Tools: VSCode | Source Code: GitHub

This Java program is designed to decode a series of instructions given as five-digit numbers. Each instruction represents a direction to turn (left or right) and the number of steps to take. The first two digits of each instruction are used to determine the direction, while the last three digits represent the number of steps. The program processes the instructions and outputs the decoded direction and steps for each line of input.

The program starts by initializing a variable previousDirection to track the direction of the previous instruction, which is important when the sum of the first two digits is zero, indicating that the direction should be the same as the previous one. The input is processed in a loop, where each line is read as a string, and if the instruction is 99999, the loop breaks, signaling the end of the input.

For each instruction, the program extracts the first two digits and calculates their sum to determine the direction. If the sum is odd, the direction is set to left. If the sum is even and not zero, the direction is set to right. If the sum is zero, the direction is taken to be the same as the previous direction. After determining the direction, the program extracts the last three digits as the number of steps to take, and then prints the decoded direction and the number of steps.

Each instruction is processed in sequence, and after each one is decoded, the previousDirection is updated to ensure that the correct direction is used if the sum of the first two digits is zero. The program ensures that the output is generated for each instruction until the special 99999 instruction is encountered, at which point the program stops.

This approach effectively decodes the instructions based on the specified rules and produces the correct output for each line of input. The program handles multiple instructions efficiently and ensures that all edge cases, such as ties in direction, are managed correctly.


SecretInstructions Java Code

SecretInstructions Java Code


            import java.util.Scanner;
    
            public class SecretInstructions {
                public static void main(String[] args) {
                    Scanner scanner = new Scanner(System.in);
    
                    String previousDirection = "";
    
                    while (true) {
                        String instruction = scanner.nextLine();
    
                        if (instruction.equals("99999")) {
                            break;
                        }
    
                        int a = Character.getNumericValue(instruction.charAt(0));
                        int b = Character.getNumericValue(instruction.charAt(1));
                        int steps = Integer.parseInt(instruction.substring(2));
    
                        String direction = "";
                        if ((a + b) % 2 == 1) {
                            direction = "left";
                        } else if (a + b != 0) {
                            direction = "right";
                        } else {
                            direction = previousDirection;
                        }
    
                        System.out.println(direction +  " " + steps);
                        previousDirection = direction;
                    }
    
                    scanner.close();
                }
            }
        

Find me on the interwebs!

Github LinkedIn Instagram Facebook