Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Advent2018;
- import AssemBunny.Command2018;
- import AssemBunny.Operation;
- import util.AdventOfCode;
- import java.util.*;
- public class Day21 extends AdventOfCode {
- public Day21(List<String> input) {
- super(input);
- }
- int pointer;
- int boundRegister;
- List<Operation> instr;
- Set<Integer> seen = new LinkedHashSet<>();
- int[] reg = new int[6];
- @Override
- public Object part1() {
- while (pointer < instr.size()) {
- if (runProgram()) break;
- }
- return seen.stream().findFirst().orElse(0);
- }
- boolean runProgram() {
- Operation op = instr.get(pointer);
- reg[boundRegister] = pointer;
- if (!op.cmd.firstReg) {
- op.aReg = reg[op.aVal];
- }
- if (!op.cmd.secondReg ) {
- op.bReg = reg[op.bVal];
- }
- reg[op.c] = op.cmd.opcode.applyAsInt(op);
- // found
- if (pointer == 28) {
- if (!seen.add(reg[1])) {
- return true;
- }
- }
- pointer = reg[boundRegister];
- pointer++;
- return false;
- }
- @Override
- public Object part2() {
- return seen.toArray()[seen.size() - 1];
- }
- @Override
- public void parse() {
- instr = new ArrayList<>();
- for (int i = 0; i < input.size(); i++) {
- String[] split = input.get(i).split(" ");
- if (i == 0) {
- boundRegister = Integer.parseInt(split[1]);
- } else {
- Command2018 cmd = Command2018.valueOf(split[0].toUpperCase());
- int a = Integer.parseInt(split[1]);
- int b = Integer.parseInt(split[2]);
- int c = Integer.parseInt(split[3]);
- Operation op = new Operation(a, a, b, b, c, cmd);
- instr.add(op);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement