Skip to content

Instantly share code, notes, and snippets.

@RShergold
Last active December 10, 2015 15:55
Show Gist options
  • Save RShergold/1d50b4980f959e0b39ad to your computer and use it in GitHub Desktop.
Save RShergold/1d50b4980f959e0b39ad to your computer and use it in GitHub Desktop.
adventofcode day 7
var lines = `123 -> x
456 -> y
x AND y -> d
x OR y -> e
x LSHIFT 2 -> f
y RSHIFT 2 -> g
NOT x -> h
NOT y -> i`.split('\n');
var wires = {};
function resolve(line) {
var operators = {'AND':'&','OR':'|','LSHIFT':'<<','RSHIFT':'>>','NOT':'~'}
line = line.replace(/AND|OR|LSHIFT|RSHIFT|NOT/, (op) => operators[op]);
var components = line.match(/^(.+) -> ([a-z]+)$/);
var command = components[1].replace(/(\w+|\d+)/g, unwrap);
if (/undefined/.test(command)) {
return false;
} else {
wires[components[2]] = eval(command) & 65535;
return true;
}
}
function unwrap(pointer) {
return ( /\d/.test(pointer) ) ? parseInt(pointer) : wires[pointer];
}
while (lines.length) {
var command = lines.shift();
if ( resolve(command) == false ) lines.push(command);
}
console.log( wires );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment