case 'RSHIFT': $this->functions[$wire] = function () use($parts) { $value = $this->getValue($parts[0]) >> $this->getValue($parts[2]); if (0 > $value) { $value += 65536; } return $value; }; break; } } } $handle = fopen("input.txt", "r"); $wires = new Wires(); if ($handle) { while (($line = fgets($handle)) !== false) { list($command, $wire) = explode(' -> ', trim($line)); $wires->add($wire, $command); } fclose($handle); } $allWires = $wires->getFunctions(); // Get the Signal of wire 'a' $signalOfA = $allWires['a'](); // Reset all the calculated values $wires->reset(); // Add a new wire 'b' with the signal of wire 'a' $wires->add('b', $signalOfA); // Get all the created functions again and calculate the signal of wire 'a' $allWires = $wires->getFunctions(); echo 'Wire "A" has signal: ' . $allWires['a']() . PHP_EOL;
$this->functions[$wire] = function () use($parts) { $value = $this->getValue($parts[0]) << $this->getValue($parts[2]); if (0 > $value) { $value += 65536; } return $value; }; break; case 'RSHIFT': $this->functions[$wire] = function () use($parts) { $value = $this->getValue($parts[0]) >> $this->getValue($parts[2]); if (0 > $value) { $value += 65536; } return $value; }; break; } } } $handle = fopen("input.txt", "r"); $wires = new Wires(); if ($handle) { while (($line = fgets($handle)) !== false) { list($command, $wire) = explode(' -> ', trim($line)); $wires->add($wire, $command); } fclose($handle); } $allWires = $wires->getFunctions(); echo 'Wire "A" has signal: ' . $allWires['a']() . PHP_EOL;