Skip to content

Instantly share code, notes, and snippets.

@thomasruiz
Created December 7, 2015 10:55
Show Gist options
  • Save thomasruiz/069d0fb9fb389b5697e1 to your computer and use it in GitHub Desktop.
Save thomasruiz/069d0fb9fb389b5697e1 to your computer and use it in GitHub Desktop.
/!\ Spoil advent of code
<?php
$i = file('i');
$z = [];
foreach ($i as $s) {
$r = explode(' -> ', $s);
$z[trim($r[1])] = trim($r[0]);
}
/* PART 2 (REMOVE THIS LINE FOR PART 1) */
$z['b'] = 46065;
function resolve($op, &$z) {
$ops = explode(' ', $op);
if (count($ops) === 1) {
if (isset($z[$ops[0]]) && !is_numeric($ops[0])) {
return $z[$ops[0]] = (int) resolve($z[$ops[0]], $z);
}
return $z[$ops[0]] = (int) $ops[0];
}
if (count($ops) === 2) {
return $z[$ops[1]] = ~ (int) resolve($ops[1], $z);
}
$a = resolve($ops[0], $z);
$b = resolve($ops[2], $z);
switch ($ops[1]) {
case 'AND':
$r = $a & $b;
break;
case 'OR':
$r = $a | $b;
break;
case 'LSHIFT':
$r = $a << $b;
break;
case 'RSHIFT':
$r = $a >> $b;
break;
default:
throw new Exception('Unknown op: ' . $ops[1]);
}
return (int) $r;
}
echo resolve($z['a'], $z);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment