Skip to content

Instantly share code, notes, and snippets.

@bernik
Last active December 7, 2015 20:24
Show Gist options
  • Save bernik/879720f4e2805000b51c to your computer and use it in GitHub Desktop.
Save bernik/879720f4e2805000b51c to your computer and use it in GitHub Desktop.
<?php
$isPartTwo = true;
function def ($name, $val) {
global $isPartTwo;
if ($name == "b" and $isPartTwo) $val = 956;
eval ("function _{$name} () { static \$res; if (!\$res) \$res = {$val}; return \$res; ;}");
};
$patterns = [
["/^(\w+) RSHIFT (\d+) -> (\w+)$/", function ($matches) { def($matches[3], "_{$matches[1]}() >> {$matches[2]}"); }],
["/^(\w+) LSHIFT (\d+) -> (\w+)$/", function ($matches) { def($matches[3], "_{$matches[1]}() << {$matches[2]}"); }],
["/^(\d+) AND (\w+) -> (\w+)$/", function ($matches) { def($matches[3], "{$matches[1]} & _{$matches[2]}()"); }],
["/^(\w+) AND (\d+) -> (\w+)$/", function ($matches) { def($matches[3], "_{$matches[1]}() & {$matches[2]}"); }],
["/^(\w+) AND (\w+) -> (\w+)$/", function ($matches) { def($matches[3], "_{$matches[1]}() & _{$matches[2]}()"); }],
["/^(\d+) OR (\w+) -> (\w+)$/", function ($matches) { def($matches[3], "{$matches[1]} | _{$matches[2]}()"); }],
["/^(\w+) OR (\d+) -> (\w+)$/", function ($matches) { def($matches[3], "_{$matches[1]}() | {$matches[2]}"); }],
["/^(\w+) OR (\w+) -> (\w+)$/", function ($matches) { def($matches[3], "_{$matches[1]}() | _{$matches[2]}()"); }],
["/^NOT (\w+) -> (\w+)$/", function ($matches) { def($matches[2], "0xFFFF & ~_{$matches[1]}()"); }],
["/^(\d+) -> (\w+)$/", function ($matches) { def($matches[2], $matches[1]); }],
["/^(\w+) -> (\w+)$/", function ($matches) { def($matches[2], "_{$matches[1]}()"); }],
];
function parseLine ($line) {
global $patterns;
$matches = [];
foreach ($patterns as list($pattern, $parser)) {
if (preg_match($pattern, $line, $matches)) {
$parser($matches);
break;
}
}
}
$input = [];
$handle = fopen("input.txt", "r+");
while (($line = fgets($handle)) !== false) $input[] = $line;
fclose($handle);
foreach ($input as $line) parseLine($line);
print_r(_a());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment