Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2015
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.function.BiFunction;
  4.  
  5. /**
  6.  * @author /u/Philboyd_Studge on 12/22/2015.
  7.  */
  8. public class Advent22 {
  9.  
  10.     static List<Instruction> program = new ArrayList<>();
  11.  
  12.     static int index;
  13.  
  14.     enum CMD {
  15.         hlf((x, y) -> x / 2),
  16.         tpl((x, y) -> x * 3),
  17.         inc((x, y) -> x + 1),
  18.         jmp(Advent22::jump),
  19.         jie(Advent22::jumpIfEven),
  20.         jio(Advent22::jumpIfOne);
  21.         private final BiFunction<Integer, Integer, Integer> func;
  22.  
  23.         private CMD(BiFunction<Integer, Integer, Integer> func) {
  24.             this.func = func;
  25.         }
  26.     }
  27.  
  28.     public static int jump(int offset, int value) {
  29.         return index + offset;
  30.     }
  31.  
  32.     public static int jumpIfEven(int offset, int value) {
  33.         return isEven(value) ? index + offset : index + 1;
  34.     }
  35.  
  36.     public static int jumpIfOne(int offset, int value) {
  37.         return value==1 ? index + offset : index + 1;
  38.     }
  39.  
  40.     public static boolean isEven(int val) {
  41.         return val % 2 == 0;
  42.     }
  43.  
  44.     static class Instruction {
  45.         CMD command;
  46.         Register register;
  47.         int offset;
  48.  
  49.         public Instruction(CMD command, Register register, int offset) {
  50.             this.command = command;
  51.             this.register = register;
  52.             this.offset = offset;
  53.         }
  54.  
  55.         public void execute() {
  56.             if (command.ordinal() < 3) {
  57.                 register.value = command.func.apply(register.value, 0);
  58.                 index++;
  59.             } else {
  60.                 index = command.func.apply(offset, register.value);
  61.             }
  62.         }
  63.     }
  64.  
  65.     static class Register {
  66.         int value = 0;
  67.     }
  68.  
  69.     public static void run() {
  70.         while (true) {
  71.             Instruction instr = program.get(index);
  72.             instr.execute();
  73.             if (index >= program.size()) break;
  74.         }
  75.     }
  76.  
  77.     public static void main(String[] args) {
  78.         List<String[]> input = FileIO.getFileLinesSplit("advent22.txt", "[ ,]+");
  79.  
  80.         Register a = new Register();
  81.         Register b = new Register();
  82.  
  83.         for (String[] each : input) {
  84.             program.add(new Instruction(CMD.valueOf(each[0]),
  85.                     each[1].equals("a") ? a : each[1].equals("b") ? b : a,
  86.                     each.length==2 && (each[1].startsWith("+") || each[1].startsWith("-"))
  87.                             ? Integer.parseInt(each[1]) : each.length==3
  88.                             ? Integer.parseInt(each[2]) : 0));
  89.         }
  90.  
  91.         run();
  92.  
  93.         System.out.println(b.value);
  94.  
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement