PHP,281字节
$b=0;$p=['|'=>'$a[$b].="F ";$f[$b]++;','/'=>'$a[$b].="R ";','\\'=>'$a[$b].="L ";','^'=>'$d.=$a[0];$n=2;$a=["R ","L "];$f=[];$b=1;','Y'=>'$d.=$a[$f[0]<$f[$n=1]]."F ";$a=[];','D'=>'$d.=$a[$b];exit(rtrim($d));'];foreach(str_split($argv[$n=1])as$c){if($x=$p[$c]){eval($x);$b=++$b%$n;}}
这是两次打高尔夫球的结果。非高尔夫版本为:
$a=$f=[]; // these assignments are not required (they were suppresed in v2)
$n=1; // this assignment can be squeezed into $argv[$n=1]
$b=0; // if this assignment is suppressed $b becomes '' and breaks the logic
$code = [
'|' => '$a[$b].="F ";$f[$b]++;',
'/' => '$a[$b].="R ";',
'\\'=> '$a[$b].="L ";',
'^' => '$d.=$a[0];$n=2;$a=["R ","L "];$f=[];$b=1;',
'Y' => '$d.=$a[$f[0]<$f[$n=1]]."F ";$a=[];',
'D' => '$d.=$a[$b];echo(rtrim($d));',
];
foreach (str_split($argv[1]) as $char) {
// ignore input characters not in the keys of $code
if ($x = $code[$char]) {
eval($x);
$b = ++ $b % $n; // cycles between 0 and 1 ($n == 2) or stays 0 ($n == 1)
}
}
它本身很漂亮,它是对以下高尔夫程序(312字节)的改进:
$b=0;foreach(str_split($argv[$n=1])as$c){if($c=='|'){$a[$b].='F ';$f[$b]++;}elseif($c=='/'){$a[$b].='R ';}elseif($c=='\\'){$a[$b].='L ';}elseif($c=='^'){$d.=$a[0];$n=2;$a=['R ','L '];$f=[];$b=1;}elseif($c==Y){$d.=$a[$f[0]<$f[$n=1]].'F ';$a=[];}elseif($c==D){$d.=$a[$b];exit(rtrim($d));}else continue;$b=++$b%$n;}
它是原始版本的高尔夫版本:
$map = $argv[1];
$dir = ''; // the already computed directions
$nb = 1; // the number of branches
$branches = [ '' ]; // the branches (2 while between '^' and 'Y', 1 otherwise)
$nbF = [ 0, 0 ]; // the number of 'F's on the branches (used to select the branch)
$curr = 0; // the current branch
foreach (str_split($map) as $char) {
if ($char == '|') { // go 'F'orward
$branches[$curr] .= 'F '; // put it to the current branch
$nbF[$curr] ++; // count it for the current branch
} elseif ($char == '/') { // go 'R'ight
$branches[$curr] .= 'R ';
} elseif ($char == '\\') { // go 'L'eft
$branches[$curr] .= 'L ';
} elseif ($char == '^') { // fork; choose the path ('L' or 'R') that contains the most 'F'orward segments
$dir .= $branches[0]; // flush the current path (it was stored as the first branch)
$nb = 2; // start two branches
$branches = [ 'R ', 'L ' ]; // put the correct directions on each branch
$nbF = [ 0, 0 ]; // no 'F's on any branch yet
$curr = 1; // need this to let it be 0 on the next loop
} elseif ($char == 'Y') { // join
$dir .= $branches[$nbF[0] < $nbF[1]]; // flush; choose the branch having the most 'F's
$dir .= 'F '; // treat it like a "|"
$branches = [ '' ]; // back to a single, empty branch
$nb = 1;
} elseif ($char == 'D') { // finish
$dir .= $branches[$curr]; // flush
break; // and exit; could use 'return' but it's one byte longer; use exit() in the final program and save 5 bytes
} else {
continue;
}
$curr = ++ $curr % $nb;
}
echo rtrim($dir);
执行示例:
$ php -d error_reporting=0 directions.php '
|
|
\
\
^
/ |
| |
\ |
\ \
\ \
\ /
Y
D
'; echo
F F L L L F F F L L R F
$
它还可以正确处理多个分支(需要在下一个分支之前加入,以便任何时候最多具有两个分支)。我在评论中询问了多个分叉,但是当答案(“不需要”)到来时,代码已经完成。
带有测试套件的完整代码和更多注释可以在github上找到。
L L,我认为应该如此L L L。与这个例子Y仍具有1在年底似乎也有其他错误。我阅读地图就像F F R R R F F F R R L F是正确理解规则一样。