protected function execute(InputInterface $input, OutputInterface $output)
 {
     $instructions = file_get_contents(__DIR__ . '/instructions.txt');
     $elevator = new Elevator();
     $instructionParser = new InstructionParser($elevator);
     $instructionParser->parseInstructions($instructions);
     $output->writeln('Puzzle 1 answer: ' . $elevator->getFloor());
 }
 /**
  * Receive update from subject
  *
  * @link http://php.net/manual/en/splobserver.update.php
  *
  * @param SplSubject $elevator
  *
  * @internal param SplSubject $subject <p>
  * The <b>SplSubject</b> notifying the observer of an update.
  * </p>
  *
  * @since 5.1.0
  */
 public function update(SplSubject $elevator)
 {
     if (!$elevator instanceof Elevator) {
         return;
     }
     if (empty($this->instructionNumber) && $elevator->getFloor() == -1) {
         $this->instructionNumber = $this->instructionParser->getInstructionCount();
     }
 }
 /**
  * Receive update from subject
  * @link http://php.net/manual/en/splobserver.update.php
  *
  * @param SplSubject $elevator <p>
  * The <b>SplSubject</b> notifying the observer of an update.
  * </p>
  *
  * @return void
  * @since 5.1.0
  */
 public function update(SplSubject $elevator)
 {
     if ($elevator instanceof Elevator) {
         if ($elevator->getFloor() == -1) {
             if (is_null($this->instructionNumber)) {
                 $this->instructionNumber = $this->parser->getInstructionCount();
             }
         }
     }
 }
 public function it_only_records_the_instruction_position_when_entering_basement_for_the_first_time(InstructionParser $parser, Elevator $elevator)
 {
     $position = random_int(1, 1000);
     $parser->getInstructionPosition()->willReturn($position);
     $elevator->getFloor()->willReturn(-1);
     $this->update($elevator);
     $this->getInstructionPosition()->shouldBe($position);
     $parser->getInstructionPosition()->willReturn($position * 2);
     $this->update($elevator);
     $this->getInstructionPosition()->shouldBe($position);
 }
 public function it_should_store_only_the_first_instruction_for_basement_entry(InstructionParser $parser, Elevator $elevator)
 {
     $count = rand(1, 10000);
     $parser->getInstructionCount()->willReturn($count);
     $elevator->getFloor()->willReturn(-1);
     $this->update($elevator);
     $this->getInstructionNumber()->shouldBe($count);
     $parser->getInstructionCount()->willReturn($count * 2);
     $this->update($elevator);
     $this->getInstructionNumber()->shouldBe($count);
 }
Example #6
0
 /**
  * @test
  * @dataProvider getInstructions
  */
 public function close_causes_entering_basement_at_position_one(string $instructions, int $expectedPosition)
 {
     $this->parser->parseInstructions($instructions);
     static::assertEquals($expectedPosition, $this->floorObserver->getInstructionPosition());
 }