Exemplo n.º 1
0
 public function __invoke()
 {
     $circuit = new Circuit();
     $input = file(__DIR__ . '/../../input/Day7/input.txt');
     foreach ($input as $connection) {
         $circuit->addGroup($connection);
     }
     echo "The value of a is " . $circuit->resolve('a') . "\n\n";
 }
Exemplo n.º 2
0
 public function __construct(Circuit &$circuit, array $signals, callable $callback)
 {
     $this->callback = $callback;
     foreach ($signals as $signal) {
         $this->signals[$signal] = false;
     }
     foreach ($signals as $signal) {
         $circuit->getOrWaitSignal($signal, [$this, 'waitCallback']);
     }
 }
Exemplo n.º 3
0
 /**
  * Determines if given circuit is equal to this circuit.
  * @param Circuit $circuit to compare
  * @return bool  true if they have the same {@link CircuitComponent}s
  */
 public function equals(Circuit $circuit)
 {
     $componentsLeft = $this->getComponents();
     $componentsRight = $circuit->getComponents();
     if (count($componentsLeft) !== count($componentsRight)) {
         return false;
     }
     foreach ($componentsLeft as $component) {
         if (!in_array($component, $componentsRight)) {
             return false;
         }
     }
     return true;
 }
Exemplo n.º 4
0
 public function __invoke()
 {
     $circuit = new Circuit(new WiresCollection());
     $parser = new InstructionsParser($circuit);
     $input = file_get_contents(__DIR__ . "/../../input/Day7/Puzzle1");
     $instructions = explode(PHP_EOL, $input);
     while (true) {
         foreach ($instructions as $k => $instruction) {
             if ($parser->parse($instruction) !== false) {
                 unset($instructions[$k]);
             }
         }
         if (empty($instructions)) {
             break;
         }
     }
     echo "Signal provided to wire 'a':" . $circuit->getWireValue("a");
 }
Exemplo n.º 5
0
 public function testNonExistentWireReturnsFalse()
 {
     $circuit = new Circuit();
     $this->assertFalse($circuit->getWire('a'), 'Getting non existent wire did not return false');
 }
Exemplo n.º 6
0
    public function output()
    {
        return $this->left->output() & $this->right->output();
    }
}
class OrGate extends Gate
{
    public function output()
    {
        return $this->left->output() | $this->right->output();
    }
}
class LShiftGate extends Gate
{
    public function output()
    {
        return $this->left->output() << $this->right->output();
    }
}
class RShiftGate extends Gate
{
    public function output()
    {
        return $this->left->output() >> $this->right->output();
    }
}
$circ = new Circuit();
$out = $circ->getNode('a')->output();
$circ->reset();
$circ->setNode('b', new RawValue($out));
echo 'Answer: a -> ' . $circ->getNode('a')->output() . PHP_EOL;
Exemplo n.º 7
0
<?php

require_once __DIR__ . '/vendor/autoload.php';
$lines = file(__DIR__ . '/input', FILE_IGNORE_NEW_LINES);
$processedIndexes = [];
$circuit = new Circuit();
$loopCount = 0;
do {
    foreach ($lines as $key => $command) {
        if (in_array($key, $processedIndexes)) {
            continue;
            // Already processed - this will make it slow, but hey! :)
        }
        $result = false;
        $cmd = (new CommandParser($command))->getCommand();
        $assignee = new Wire($cmd['assignee']);
        switch ($cmd['type']) {
            case 'ASSIGN':
                if (is_numeric($cmd['value'])) {
                    $result = $cmd['value'];
                } else {
                    $wire1 = $circuit->getWire($cmd['value']);
                    if ($wire1 === false) {
                        break;
                    }
                    $result = $wire1->getSignal();
                }
                break;
            case 'AND':
                if (is_numeric($cmd['wires'][0])) {
                    $wire1 = new Wire('zz', $cmd['wires'][0]);
 public function parse($instruction)
 {
     list($sources, $target) = explode(" -> ", $instruction);
     if (is_numeric($sources)) {
         return $this->circuit->sendSignal($target, $sources);
     }
     $parts = explode(" ", $sources);
     if (count($parts) == 1) {
         $value = $this->circuit->getWireValue($sources);
         if (is_null($value)) {
             return false;
         }
         return $this->circuit->sendSignal($target, $value);
     }
     if (count($parts) == 2) {
         list($not, $source) = $parts;
         $value = $this->circuit->getWireValue($source);
         if (is_null($value)) {
             return false;
         }
         return $this->circuit->sendSignal($target, ~$value);
     }
     list($source1, $type, $source2) = $parts;
     $source1 = is_numeric($source1) ? $source1 : $this->circuit->getWireValue($source1);
     $source2 = is_numeric($source2) ? $source2 : $this->circuit->getWireValue($source2);
     if (is_null($source1) || is_null($source2)) {
         return false;
     }
     switch ($type) {
         case "AND":
             $this->circuit->sendSignal($target, $source1 & $source2);
             break;
         case "OR":
             $this->circuit->sendSignal($target, $source1 | $source2);
             break;
         case "LSHIFT":
             $this->circuit->sendSignal($target, $source1 << $source2);
             break;
         case "RSHIFT":
             $this->circuit->sendSignal($target, $source1 >> $source2);
             break;
     }
 }
 /**
  * @test
  * @covers ::parse
  * @covers ::getWireOrSignalGenerator
  * @covers ::getWire
  * @covers ::parseAndOrGate
  */
 public function parsesAndGateWithTwoSignals()
 {
     $input = "1 AND cx -> cy";
     $signal = new SignalGenerator(1);
     $wireCx = new Wire('cx');
     $andGate = new GateAnd($signal, $wireCx);
     $wireCy = new Wire('cy');
     $wireCy->setSource($andGate);
     $components = [$signal, $wireCx, $wireCy, $andGate];
     $circuit = new Circuit($components);
     $parsedCircuit = CircuitInstructionParser::parse($input);
     $this->assertTrue($circuit->equals($parsedCircuit));
 }
Exemplo n.º 10
0
<?php

//middleware solution
$wires = [];
$instructions = ['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'];
//$instructions = file('7.txt');
$circuit = new Circuit();
foreach ($instructions as $instruction) {
    $circuit->processInstruction($instruction);
}
print_r($circuit->getWires());
class Circuit
{
    private $wires = [];
    public function processInstruction($instruction)
    {
        preg_match('/^(?<a>.* ?)(?<operator>(AND|OR|LSHIFT|RSHIFT|NOT))? ?(?<b>.*) -> (?<wire>.*)$/', $instruction, $matches);
        print_r($matches);
        // We are dealing with a gate
        if (!empty($matches['operator'])) {
            $gate = new LogicGate();
            $input = $gate->process($matches['operator'], $this->getValue($matches['a']), $this->getValue($matches['b']));
        } else {
            $input = !empty($matches['a']) ? $matches['a'] : $matches['b'];
            $input = $this->getValue($input);
        }
        $this->wires[$matches['wire']] = $input;
    }
    public function getValue($input)
    {
        return is_numeric($input) ? (int) $input : $this->getWireValue($input);
Exemplo n.º 11
0
 /**
  * @test
  * @covers ::equals
  */
 public function comparesDifferentCircuits()
 {
     $circuitOne = new Circuit([new Wire('a'), new Wire('b')]);
     $circuitTwo = new Circuit([new Wire('c'), new Wire('d')]);
     $this->assertFalse($circuitOne->equals($circuitTwo));
 }