Skip to content

Instantly share code, notes, and snippets.

@devNoiseConsulting
Created January 3, 2018 03:27
Show Gist options
  • Select an option

  • Save devNoiseConsulting/1e8938c03ef40c221a1df4a9f8dfe43d to your computer and use it in GitHub Desktop.

Select an option

Save devNoiseConsulting/1e8938c03ef40c221a1df4a9f8dfe43d to your computer and use it in GitHub Desktop.
Duet - Advent of Code - 20171218
let duet = function(instructions) {
let offset = 'a'.charCodeAt();
let registers = new Array(26).fill(0);
let played = 0;
let recovered = 0;
let index = 0;
let executeInstruction = function(instruction) {
let [operation, a, b] = instruction.split(' ');
let aIndex = a.charCodeAt() - offset;
a = parseInt(a);
if (Number.isNaN(a)) {
a = registers[aIndex];
}
if (b) {
let bIndex = b.charCodeAt() - offset;
b = parseInt(b);
if (Number.isNaN(b)) {
b = registers[bIndex];
}
}
switch (operation) {
case 'snd':
played = registers[aIndex];
sent++;
break;
case 'set':
registers[aIndex] = b;
break;
case 'add':
registers[aIndex] = a + b;
break;
case 'mul':
registers[aIndex] = a * b;
break;
case 'mod':
registers[aIndex] = a % b;
break;
case 'rcv':
if (registers[aIndex] > 0) {
recovered = played;
}
break;
case 'jgz':
if (a > 0) {
index += b - 1;
}
break;
}
index++;
};
while (index < instructions.length) {
executeInstruction(instructions[index]);
if (recovered) {
return recovered;
}
}
return recovered;
};
let duet2 = function(instructions) {
let offset = 'a'.charCodeAt();
let registers0 = new Array(26).fill(0);
let registers1 = new Array(26).fill(0);
let index0 = 0;
let index1 = 0;
let sent0 = 0;
let sent1 = 0;
let queue0 = [];
let queue1 = [];
let waiting0 = false;
let waiting1 = false;
let executeInstruction = function(
instruction,
registers,
index,
queue,
sent,
waiting
) {
let [operation, a, b] = instruction.split(' ');
let aIndex = a.charCodeAt() - offset;
a = parseInt(a);
if (Number.isNaN(a)) {
a = registers[aIndex];
}
if (b) {
let bIndex = b.charCodeAt() - offset;
b = parseInt(b);
if (Number.isNaN(b)) {
b = registers[bIndex];
}
}
switch (operation) {
case 'snd':
played = registers[aIndex];
queue.push(registers[aIndex]);
sent++;
break;
case 'set':
registers[aIndex] = b;
break;
case 'add':
registers[aIndex] = a + b;
break;
case 'mul':
registers[aIndex] = a * b;
break;
case 'mod':
registers[aIndex] = a % b;
break;
case 'rcv':
if (queue.length == 0) {
index--;
waiting = true;
} else {
registers[aIndex] = queue.pop();
waiting = false;
}
break;
case 'jgz':
if (a > 0) {
index += b - 1;
}
break;
}
index++;
return [registers, index, queue, sent, waiting];
};
while (index0 < instructions.length) {
[registers0, index0, queue1, sent0, waiting0] = executeInstruction(
instructions[index0],
registers0,
index0,
queue1,
sent0,
waiting0
);
[registers1, index1, queue0, sent1, waiting1] = executeInstruction(
instructions[index1],
registers1,
index1,
queue0,
sent1,
waiting1
);
if (waiting0 && waiting1) {
return [recovered, sent1];
}
}
return [recovered, sent];
};
let test = `set i 31
set a 1
mul p 17
jgz p p
mul a 2
add i -1
jgz i -2
add a -1
set i 127
set p 826
mul p 8505
mod p a
mul p 129749
add p 12345
mod p a
set b p
mod b 10000
snd b
add i -1
jgz i -9
jgz a 3
rcv b
jgz b -1
set f 0
set i 126
rcv a
rcv b
set p a
mul p -1
add p b
jgz p 4
snd a
set a b
jgz 1 3
snd b
set f 1
add i -1
jgz i -11
snd a
jgz f -16
jgz a -19`.split('\n');
/*
test = `set a 1
add a 2
mul a a
mod a 5
snd a
set a 0
rcv a
jgz a -1
set a 1
jgz a -2`.split('\n');
*/
let result = duet2(test);
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment