Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.14 KB | None | 0 0
  1. package Advent2018;
  2.  
  3. import util.AdventOfCode;
  4. import util.FileIO;
  5.  
  6. import java.util.*;
  7. import java.util.function.IntBinaryOperator;
  8. import java.util.function.ToIntFunction;
  9.  
  10. public class Day16 extends AdventOfCode {
  11.  
  12.     public Day16(List<String> input) {
  13.         super(input);
  14.         title = "Chronal Classification";
  15.         part1Description = "Samples with 3 or more valid opcodes: ";
  16.         part2Description = "Value at register 0 after running test program: ";
  17.     }
  18.  
  19.     class Sample {
  20.         int[] before = new int[4];
  21.         int[] after = new int[4];
  22.         int[] codes = new int[4];
  23.     }
  24.  
  25.     List<Sample> samples;
  26.     List<int[]> program;
  27.  
  28.     class Operation {
  29.         int aReg;
  30.         int aVal;
  31.         int bReg;
  32.         int bVal;
  33.         int c;
  34.         Command cmd;
  35.  
  36.         public Operation(int aReg, int aVal, int bReg, int bVal, int c, Command cmd) {
  37.             this.aReg = aReg;
  38.             this.aVal = aVal;
  39.             this.bReg = bReg;
  40.             this.bVal = bVal;
  41.             this.c = c;
  42.             this.cmd = cmd;
  43.         }
  44.     }
  45.  
  46.  
  47.     enum Command {
  48.         EQRI(Command::eq, Command::ri),
  49.         BANI((x, y) -> x & y, Command::ri),
  50.         SETI((x, y) -> x, Command::ir),
  51.         BORI((x, y) -> x | y, Command::ri),
  52.         EQIR(Command::eq, Command::ir),
  53.         BANR((x, y) -> x & y, Command::rr),
  54.         BORR((x, y) -> x | y, Command::rr),
  55.         MULI((x, y) -> x * y, Command::ri),
  56.         SETR((x, y) -> x, Command::rr),
  57.         ADDR((x, y) -> x + y, Command::rr),
  58.         EQRR(Command::eq, Command::rr),
  59.         ADDI((x, y) -> x + y, Command::ri),
  60.         GTIR(Command::gt, Command::ir),
  61.         GTRR(Command::gt, Command::rr),
  62.         GTRI(Command::gt, Command::ri),
  63.         MULR((x, y) -> x * y, Command::rr);
  64.  
  65.  
  66.         IntBinaryOperator function;
  67.         ToIntFunction<Operation> opcode;
  68.         Command(IntBinaryOperator function, ToIntFunction<Operation> opcode) {
  69.             this.function = function;
  70.             this.opcode = opcode;
  71.         }
  72.  
  73.  
  74.         static int gt(int x, int y) {
  75.             return x > y ? 1 : 0;
  76.         }
  77.  
  78.         static int eq(int x, int y) {
  79.             return x == y ? 1 : 0;
  80.         }
  81.  
  82.         static int rr(Operation op) {
  83.             return op.cmd.function.applyAsInt(op.aReg, op.bReg);
  84.         }
  85.  
  86.         static int ri(Operation op) {
  87.             return op.cmd.function.applyAsInt(op.aReg, op.bVal);
  88.         }
  89.  
  90.         static int ir(Operation op) {
  91.             return op.cmd.function.applyAsInt(op.aVal, op.bReg);
  92.         }
  93.  
  94.     }
  95.  
  96.     void run(int[] reg, int[] codes, Command cmd) {
  97.         int a = codes[1];
  98.         int b = codes[2];
  99.         int c = codes[3];
  100.  
  101.         Operation op = new Operation(reg[a], a, reg[b], b, reg[c], cmd);
  102.         reg[codes[3]] = cmd.opcode.applyAsInt(op);
  103.     }
  104.  
  105.     @Override
  106.     public Object part1() {
  107.         int hasthree = 0;
  108.  
  109.         // use this to order enums for part 2
  110.         Map<Integer, Set<Command>> cmdmap = new HashMap<>();
  111.  
  112.         for (Sample sample : samples) {
  113.             int count = 0;
  114.             for (Command cmd : Command.values()) {
  115.                 int[] copy = new int[sample.before.length];
  116.                 System.arraycopy(sample.before, 0, copy, 0, sample.before.length);
  117.                 run(copy, sample.codes, cmd);;
  118.                 if (Arrays.equals(copy, sample.after)) {
  119.                     cmdmap.putIfAbsent(sample.codes[0], new HashSet<>());
  120.                     cmdmap.get(sample.codes[0]).add(cmd);
  121.                     count++;
  122.                 }
  123.             }
  124.             if (count > 2) hasthree++;
  125.         }
  126.         return hasthree;
  127.     }
  128.  
  129.     @Override
  130.     public Object part2() {
  131.         int[] register = { 0, 0, 0, 0 };
  132.         for (int[] line : program) {
  133.             run(register, line, Command.values()[line[0]]);
  134.         }
  135.         return register[0];
  136.     }
  137.  
  138.     @Override
  139.     public void parse() {
  140.         samples = new ArrayList<>();
  141.         program = new ArrayList<>();
  142.         boolean part2 = false;
  143.         Sample sample = new Sample();
  144.         samples.add(sample);
  145.         for (String line : input) {
  146.             if (line.equals("stop")) {
  147.                 part2 = true;
  148.                 continue;
  149.             }
  150.             if (part2) {
  151.                 program.add(FileIO.StringArrayToInt(line.split(" ")));
  152.             } else {
  153.                 if (line.isEmpty()) {
  154.                     sample = new Sample();
  155.                     samples.add(sample);
  156.                     continue;
  157.                 }
  158.                 if (line.startsWith("Before")) {
  159.                     String[] split = line.substring(9, line.indexOf(']')).split(", ");
  160.                     sample.before = FileIO.StringArrayToInt(split);
  161.                 } else {
  162.                     if (line.startsWith("After")) {
  163.                         String[] split = line.substring(9, line.indexOf(']')).split(", ");
  164.                         sample.after = FileIO.StringArrayToInt(split);
  165.                     } else {
  166.                         sample.codes = FileIO.StringArrayToInt(line.split(" "));
  167.                     }
  168.                 }
  169.             }
  170.  
  171.         }
  172.     }
  173.  
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement