Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class Day8 {
  5.     private static void updateRegister(HashMap<String, Integer> register, String[] instruction) {
  6.         if ("inc" == instruction[1]) {
  7.             register.put(instruction[0], register.get(instruction[0]) + Integer.parseInt(instruction[2]));
  8.         } else {
  9.             register.put(instruction[0], register.get(instruction[0]) - Integer.parseInt(instruction[2]));
  10.         }
  11.     }
  12.  
  13.     public static void main(String[] args) throws FileNotFoundException {
  14.         Scanner sc = new Scanner(new File("Day8_input"));
  15.         HashMap<String, Integer> register = new HashMap<String, Integer>();
  16.         String[] instruction = new String[7];
  17.         while (sc.hasNext()) {
  18.             instruction = sc.nextLine().split(" ");
  19.             // System.out.println(Arrays.toString(instruction));
  20.             if (!register.keySet().contains(instruction[0])) {
  21.                 register.put(instruction[0], 0);
  22.             }
  23.             if (!register.keySet().contains(instruction[4])) {
  24.                 register.put(instruction[4], 0);
  25.             }
  26.             switch (instruction[5]) {
  27.                 case "<":
  28.                     if (register.get(instruction[4]) < Integer.parseInt(instruction[6])) {
  29.                         updateRegister(register, instruction);
  30.                     }
  31.                     //System.out.println("<");
  32.                     break;
  33.                 case ">":
  34.                     if (register.get(instruction[4]) > Integer.parseInt(instruction[6])) {
  35.                         updateRegister(register, instruction);
  36.                     }
  37.                     //System.out.println(">");
  38.                     break;
  39.                 case "==":
  40.                     if (register.get(instruction[4]) == Integer.parseInt(instruction[6])) {
  41.                         updateRegister(register, instruction);
  42.                     }
  43.                     break;
  44.                 case ">=":
  45.                     if (register.get(instruction[4]) >= Integer.parseInt(instruction[6])) {
  46.                         updateRegister(register, instruction);
  47.                     }
  48.                     // System.out.println(">=");
  49.                     break;
  50.                 case "<=":
  51.                     if (register.get(instruction[4]) <= Integer.parseInt(instruction[6])) {
  52.                         updateRegister(register, instruction);
  53.                     }
  54.                     break;
  55.                 default:
  56.                     if (register.get(instruction[4]) != Integer.parseInt(instruction[6])) {
  57.                         updateRegister(register, instruction);
  58.                     }
  59.                     break;
  60.             }
  61.         }
  62.         System.out.println(Collections.max(register.values()));
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement