Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- class Day8 {
- private static void updateRegister(HashMap<String, Integer> register, String[] instruction) {
- if ("inc" == instruction[1]) {
- register.put(instruction[0], register.get(instruction[0]) + Integer.parseInt(instruction[2]));
- } else {
- register.put(instruction[0], register.get(instruction[0]) - Integer.parseInt(instruction[2]));
- }
- }
- public static void main(String[] args) throws FileNotFoundException {
- Scanner sc = new Scanner(new File("Day8_input"));
- HashMap<String, Integer> register = new HashMap<String, Integer>();
- String[] instruction = new String[7];
- while (sc.hasNext()) {
- instruction = sc.nextLine().split(" ");
- // System.out.println(Arrays.toString(instruction));
- if (!register.keySet().contains(instruction[0])) {
- register.put(instruction[0], 0);
- }
- if (!register.keySet().contains(instruction[4])) {
- register.put(instruction[4], 0);
- }
- switch (instruction[5]) {
- case "<":
- if (register.get(instruction[4]) < Integer.parseInt(instruction[6])) {
- updateRegister(register, instruction);
- }
- //System.out.println("<");
- break;
- case ">":
- if (register.get(instruction[4]) > Integer.parseInt(instruction[6])) {
- updateRegister(register, instruction);
- }
- //System.out.println(">");
- break;
- case "==":
- if (register.get(instruction[4]) == Integer.parseInt(instruction[6])) {
- updateRegister(register, instruction);
- }
- break;
- case ">=":
- if (register.get(instruction[4]) >= Integer.parseInt(instruction[6])) {
- updateRegister(register, instruction);
- }
- // System.out.println(">=");
- break;
- case "<=":
- if (register.get(instruction[4]) <= Integer.parseInt(instruction[6])) {
- updateRegister(register, instruction);
- }
- break;
- default:
- if (register.get(instruction[4]) != Integer.parseInt(instruction[6])) {
- updateRegister(register, instruction);
- }
- break;
- }
- }
- System.out.println(Collections.max(register.values()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement