Skip to content

Instantly share code, notes, and snippets.

@snarkbait
Last active December 23, 2017 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snarkbait/9ca87bfb2ab4ae1eb1822b4723f91d5a to your computer and use it in GitHub Desktop.
Save snarkbait/9ca87bfb2ab4ae1eb1822b4723f91d5a to your computer and use it in GitHub Desktop.
Advent of Code 2017 - Day 23
package Advent2017;
import java.util.function.LongBinaryOperator;
public enum Command {
snd((x, y) -> x),
add((x, y) -> x + y),
sub((x, y) -> x - y),
mul((x, y) -> x * y),
set((x, y) -> y),
mod((x, y) -> x % y),
rcv((x, y) -> x),
jgz((x, y) -> x > 0 ? y : 1),
npr((x, y) -> Command.isComposite((int) y) ? 0 : 1),
jnz((x, y) -> x != 0 ? y : 1);
LongBinaryOperator func;
Command(LongBinaryOperator func) { this.func = func;}
long apply(long x, long y) {
return this.func.applyAsLong(x, y);
}
static boolean isComposite(int n) {
for (int i = 2; i * i <= n ; i++) {
if (n % i == 0) return true;
}
return false;
}
}
package Advent2017;
import util.AdventOfCode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Day23 extends AdventOfCode {
Map<Character, Long> registers;
List<Instruction> instructions;
int position;
int mulcount;
public Day23(List<String> input) {
super(input);
part1Description = "Number of times multiply is called: ";
part2Description = "Value of register 'h': ";
}
private void process() {
while (position < instructions.size()) {
Instruction current = instructions.get(position);
long y = current.y;
long x = current.x;
if (current.toRegister != 'x') y = registers.get(current.toRegister);
if (current.register != 'x') x = registers.get(current.register);
long result = current.cmd.apply(x, y);
if (current.cmd == Command.mul) mulcount++;
switch (current.cmd) {
case jnz:
position += result;
break;
default:
registers.put(current.register, result);
position++;
}
}
}
@Override
public Object part1() {
process();
return mulcount;
}
@Override
public Object part2() {
parse();
registers.put('a', 1L);
process();
return registers.get('h');
}
@Override
public void parse() {
position = 0;
instructions = new ArrayList<>();
registers = new HashMap<>();
for (String each : input) {
String[] instr = each.split(" ");
Command cmd = Command.valueOf(instr[0]);
char register = 'x';
long x = 0;
try {
x = (long) Integer.parseInt(instr[1]);
} catch (NumberFormatException e) {
register = instr[1].charAt(0);
registers.putIfAbsent(register, 0L);
}
long y = 0;
char toRegister = 'x';
if (instr.length > 2) {
try {
y = (long) Integer.parseInt(instr[2]);
} catch (NumberFormatException e) {
toRegister = instr[2].charAt(0);
}
}
instructions.add(new Instruction(cmd, register, x, y, toRegister));
}
}
}
package Advent2017;
public class Instruction {
Command cmd;
char register;
long x;
long y;
char toRegister;
public Instruction(Command cmd, char register, long x, long y, char toRegister) {
this.cmd = cmd;
this.register = register;
this.x = x;
this.y = y;
this.toRegister = toRegister;
}
}
set b 65
set c b
jnz a 2
jnz 1 6
mul b 100
sub b -100000
set c b
sub c -17000
jnz 1 25
set f 1
set d 2
set e 2
set g d
mul g e
sub g b
jnz g 2
set f 0
sub e -1
set g e
sub g b
jnz g -8
sub d -1
set g d
sub g b
jnz g -13
jnz f 2
sub h -1
set g b
sub g c
jnz g 2
jnz 1 12
sub b -17
jnz 1 -23
npr g b
jnz g 2
sub h -1
set g b
sub g c
jnz g 2
jnz 1 3
sub b -17
jnz 1 -8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment