public function testWireCountIsZeroToStart() { $circuit = new Circuit(); $this->assertCount(0, $circuit->getWires()); }
<?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);