Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.70 KB | None | 0 0
  1. <?php
  2.  
  3. function getOpcode($value)
  4. {
  5.     [$mode3, $mode2, $mode1, $op1, $op2] = str_split(
  6.         str_pad($value, 5, '0', STR_PAD_LEFT)
  7.     );
  8.     return [$op1 . $op2, $mode1, $mode2, $mode3];
  9. }
  10.  
  11. function getValue($mem, $src, $mode, $base)
  12. {
  13.     if ($mode === '1') {
  14.         return $src;
  15.     }
  16.  
  17.     if ($mode === '2') {
  18.         return $mem[$base + $src] ?? '0';
  19.     }
  20.     return $mem[$src] ?? '0';
  21. }
  22.  
  23. function getLocation($src, $mode, $base)
  24. {
  25.     if ($mode === '2') {
  26.         return $base + $src;
  27.     }
  28.  
  29.     return $src;
  30. }
  31.  
  32. function work(&$mem, &$i, &$base, $input)
  33. {
  34.     while ($i < count($mem)) {
  35.         [$opcode, $mode1, $mode2, $mode3] = getOpcode($mem[$i]);
  36.         if ($opcode === '01') {
  37.             [$src1, $src2, $dest] = array_slice($mem, $i + 1, 3);
  38.             $mem[getLocation($dest, $mode3, $base)] =
  39.                 getValue($mem, $src1, $mode1, $base) +
  40.                 getValue($mem, $src2, $mode2, $base);
  41.             $i += 4;
  42.         } elseif ($opcode === '02') {
  43.             [$src1, $src2, $dest] = array_slice($mem, $i + 1, 3);
  44.             $mem[getLocation($dest, $mode3, $base)] =
  45.                 getValue($mem, $src1, $mode1, $base) *
  46.                 getValue($mem, $src2, $mode2, $base);
  47.             $i += 4;
  48.         } elseif ($opcode === '03') {
  49.             $dest = $mem[$i + 1];
  50.             $mem[getLocation($dest, $mode1, $base)] = $input;
  51.             $i += 2;
  52.         } elseif ($opcode === '04') {
  53.             $src1 = $mem[$i + 1];
  54.             $i += 2;
  55.             return getValue($mem, $src1, $mode1, $base);
  56.         } elseif ($opcode === '05') {
  57.             [$src1, $src2] = array_slice($mem, $i + 1, 2);
  58.             $val1 = getValue($mem, $src1, $mode1, $base);
  59.             if ($val1 != '0') {
  60.                 $i = getValue($mem, $src2, $mode2, $base);
  61.             } else {
  62.                 $i += 3;
  63.             }
  64.         } elseif ($opcode === '06') {
  65.             [$src1, $src2] = array_slice($mem, $i + 1, 2);
  66.             $val1 = getValue($mem, $src1, $mode1, $base);
  67.             if ($val1 == '0') {
  68.                 $i = getValue($mem, $src2, $mode2, $base);
  69.             } else {
  70.                 $i += 3;
  71.             }
  72.         } elseif ($opcode === '07') {
  73.             [$src1, $src2, $dest] = array_slice($mem, $i + 1, 3);
  74.             $dest = getLocation($dest, $mode3, $base);
  75.             if (
  76.                 getValue($mem, $src1, $mode1, $base) <
  77.                 getValue($mem, $src2, $mode2, $base)
  78.             ) {
  79.                 $mem[$dest] = '1';
  80.             } else {
  81.                 $mem[$dest] = '0';
  82.             }
  83.             $i += 4;
  84.         } elseif ($opcode === '08') {
  85.             [$src1, $src2, $dest] = array_slice($mem, $i + 1, 3);
  86.             $dest = getLocation($dest, $mode3, $base);
  87.             if (
  88.                 getValue($mem, $src1, $mode1, $base) ==
  89.                 getValue($mem, $src2, $mode2, $base)
  90.             ) {
  91.                 $mem[$dest] = '1';
  92.             } else {
  93.                 $mem[$dest] = '0';
  94.             }
  95.             $i += 4;
  96.         } elseif ($opcode === '09') {
  97.             $src1 = $mem[$i + 1];
  98.             $val = getValue($mem, $src1, $mode1, $base);
  99.             $base += $val;
  100.             $i += 2;
  101.         } elseif ($opcode === '99') {
  102.             return -1;
  103.         } else {
  104.             echo "UNKNOWN OPCODE" . PHP_EOL;
  105.             break;
  106.         }
  107.     }
  108. }
  109. $input = trim(file_get_contents('input'));
  110.  
  111. $mem = explode(',', $input);
  112.  
  113. $i = 0;
  114. $base = 0;
  115. $x = 0;
  116.  
  117. $result = 0;
  118. $direction = 'up';
  119. $color = 0;
  120. $position = [0, 0];
  121. $panels = [];
  122. $panels[0][0] = 1;
  123. $count = 0;
  124. while (1) {
  125.     $result = work($mem, $i, $base, getColor($panels, $position));
  126.     if ($result === -1) {
  127.         break;
  128.     }
  129.     if (!$x) {
  130.         $color = $result;
  131.     } else {
  132.         $direction = switchDirection($direction, $result);
  133.     }
  134.     $x += 1;
  135.     if ($x === 2) {
  136.         $x = 0;
  137.         $count += paint($panels, $position, $color);
  138.         $position = move($position, $direction);
  139.     }
  140. }
  141.  
  142. printMap($panels);
  143.  
  144. function getColor($panels, $position)
  145. {
  146.     [$x, $y] = $position;
  147.     return $panels[$y][$x] ?? 0;
  148. }
  149.  
  150. function paint(&$panels, $position, $color)
  151. {
  152.     [$x, $y] = $position;
  153.     $result = isset($panels[$y][$x]) ? 0 : 1;
  154.     $panels[$y][$x] = $color;
  155.     return $result;
  156. }
  157.  
  158. function move($position, $direction)
  159. {
  160.     [$x, $y] = $position;
  161.     switch ($direction) {
  162.         case 'up':
  163.             return [$x, $y - 1];
  164.         case 'right':
  165.             return [$x + 1, $y];
  166.         case 'down':
  167.             return [$x, $y + 1];
  168.         case 'left':
  169.             return [$x - 1, $y];
  170.     }
  171. }
  172.  
  173. function switchDirection($direction, $result)
  174. {
  175.     switch ($direction) {
  176.         case 'up':
  177.             return !$result ? 'left' : 'right';
  178.         case 'right':
  179.             return !$result ? 'up' : 'down';
  180.         case 'down':
  181.             return !$result ? 'right' : 'left';
  182.         case 'left':
  183.             return !$result ? 'down' : 'up';
  184.     }
  185. }
  186.  
  187. function printMap($map)
  188. {
  189.     $minY = min(array_keys($map));
  190.     $maxY = max(array_keys($map));
  191.     $minX = PHP_INT_MAX;
  192.     $maxX = PHP_INT_MIN;
  193.     foreach ($map as $y => $xValues) {
  194.         $myMin = min(array_keys($xValues));
  195.         $myMax = max(array_keys($xValues));
  196.         if ($myMin < $minX) {
  197.             $minX = $myMin;
  198.         }
  199.         if ($myMax > $maxX) {
  200.             $maxX = $myMax;
  201.         }
  202.     }
  203.  
  204.     for ($y = $minY; $y <= $maxY; $y += 1) {
  205.         for ($x = $minX; $x <= $maxX; $x += 1) {
  206.             $color = $map[$y][$x] ?? 0;
  207.             if (!$color) {
  208.                 echo ' ';
  209.             } else {
  210.                 echo '#';
  211.             }
  212.         }
  213.         echo "\n";
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement