Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Enter your code here, enjoy!
- $input_string = '1 * 1 1';
- $input_string_array = explode(' ', $input_string);
- $cassette = array_merge(['a0'], $input_string_array, ['a0']);
- $position = count($input_string_array) ;
- $table = [
- 'q1' => ['', ['q2', 'a0', 'l'], ['q0', 'a0', 's']],//['q3', '1', 'r'], ['q1', 'a0', 'l']
- 'q2' => [['q3', '1', 'r'], ['q2', '1', 'l'], ['q2', '*', 'l']],
- 'q3' => [['q1', 'a0', 'l'], ['q3', '1', 'r'], ['q3', '*', 'r']]
- ];
- $q = 'q1';
- $count_iter = 0;
- //echo $cassette[$position] . ' ' . $q . PHP_EOL;
- //print_r($table[$q][$cassette[$position]]);
- while($q != 'q0') {
- if($count_iter > 500)
- break;
- $count_iter++;
- $current = $table[$q][toRow($cassette[$position])];
- $cassette[$position] = $current[1];
- $q = $current[0];
- if($current[2] == 'l') {
- $position--;
- }elseif($current[2] == 'r') {
- $position++;
- }
- if($position < 0) {
- $cassette = array_merge(['a0'], $cassette);
- $position = 0;
- }
- if($position > count($cassette))
- $cassette = array_merge($cassette, ['a0']);
- echo $count_iter . ') Cassette: ' . implode(' ', $cassette) . ' (' . $q . ')' . PHP_EOL;
- }
- function toRow($name) {
- $rows = ['a0' => 0, '1' => 1, '*' => 2];
- return $rows[$name];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement