Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Fill the below function
- * You'll be scored on your function's character count of the line(s) between the brackets
- * Bragging rights for lowest run time of the line(s) between the brackets
- * You cannot use any existing library that solves the heart of the challenge
- * You cannot read from your own source code
- * All code must appear between the braces of your main function
- */
- /**
- * Given a 24 hour clock set at 00:00 represented as two inputs: hours and minutes
- * Given a string of {^,v,<,>}
- * - The up arrow (^) will increase the currently active input.
- * - The down arrow (v) will decrease the active input.
- * - The right arrow (>) will move focus to the right input.
- * - The left arrow (<) will move focus to the left input.
- * - Up and down movement will loop around as expected for a time input.
- * - Left and right movement don't loop around.
- * The clock starts out at 00:00 with the hour input active (see first schematic).
- * Given a list of input commands, output the resulting time in HH:mm format.
- *
- * @param string $c Input commands
- *
- * @return string Resulting time
- */
- function codegolf47_fake($c) {
- return $GLOBALS['expect'];
- }
- function _codegolf47_tyler($c) {
- $r=[$p=0,$i=0];
- for(;$c[$i];$i++)
- switch($c[$i]){
- case'>':$p=1;break;
- case'<':$p=0;break;
- case'^':$r[$p]=$r[$p]++;break;
- case'v':$r[$p]=$r[$p]--;break;
- }
- while(0>$r[0]+=24);
- while(0>$r[1]+=60);
- return($r[0]%24).':'.($r[1]%60);
- }
- function _codegolf47_tyler2($c) {
- $r=[$p=0,$i=0];
- for(;$C=$c[$i];$i++)
- if('>'==$C)$p=1;
- else if('<'==$C)$p=0;
- else if('^'==$C)$r[$p]++;
- else if('v'==$C)$r[$p]--;
- while(0>$r[0]+=24);
- while(0>$r[1]+=60);
- return($r[0]%24).':'.($r[1]%60);
- }
- function codegolf47_tyler_poly($c) {
- for($r=[$p=0,$i=0];$C=$c[$i++];)if('>'==$C)$p=1;else if('<'==$C)$p=0;else if('^'==$C)$r[$p]++;else if('v'==$C)$r[$p]--;if('\0'=="\0")eval("sprintf=(a,b,c)=>(b+':').padStart(3,0)+(c+'').padStart(2,0);");return sprintf("%02s:%02s",(96+$r[0])%24,(60+$r[1])%60);
- }
- function codegolf47_tyler_poly3_coop($c) {
- for($r=[$p=0,$i=0];$C=$c[$i++];)'>'==$C?($p=1):('<'==$C?($p=0):('^'==$C?$r[$p]++:$r[$p]--));$R=[($h=(96+$r[0])%24)<9?0:'',$h,':',($m=(60+$r[1])%60)<9?0:'',$m];return'\0'=="\0"?$R.join(''):join('',$R);
- }
- function _codegolf47_tyler_poly3($c) {
- for ($r = [$p = 0, $i = 0]; $C = $c[$i++];) {
- if ('>' == $C) {
- $p = 1;
- } else {
- if ('<' == $C) {
- $p = 0;
- } else {
- if ('^' == $C) {
- $r[$p]++;
- } else {
- if ('v' == $C) {
- $r[$p]--;
- }
- }
- }
- }
- }
- $R = [($h = (96 + $r[0]) % 24) < 9 ? 0 : '', $h, ':', ($m = (60 + $r[1]) % 60) < 9 ? 0 : '', $m];
- return '\0' == "\0" ? $R.join('') : join('', $R);
- }
- function codegolf47_tyler_php($c) {
- for($r=[$p=0,$i=0];$C=$c[$i];$i++)if('>'==$C)$p=1;elseif('<'==$C)$p=0;elseif('^'==$C)$r[$p]++;elseif('v'==$C)$r[$p]--;return sprintf("%02s:%02s",(96+$r[0])%24,(60+$r[1])%60);
- }
- function _codegolf47_tw2($c) {
- $b = 0;
- $l = 0;
- @$p = str_replace;
- $a = explode(" ", $c);
- $a = $p('^', 0, $a);
- $a = $p('v', 1, $a);
- $a = $p('<', 2, $a);
- $a = $p('>', 3, $a);
- $t = 0;
- foreach ($a as $k) {
- if ($k == 2) {
- $t = 0;
- }
- if ($k == 3) {
- $t = 1;
- }
- if ($t && $k == 0) {
- $b = $b + 1;
- }
- if ($t == 0 && $k == 1) {
- if ($b == 0) {
- $b = 23;
- }
- $b = $b - 1;
- }
- if ($t == 1 && $k == 0) {
- $l = $l + 1;
- }
- if ($t && $k == 1) {
- if ($l == 0) {
- $l = 60;
- }
- $l = $l - 1;
- }
- }
- return '0'.$b.':'.'0'.$l;
- }
- function codegolf47_karl4($c) {
- $f=0;$t=[0,0];$l=[23,59];for($x=0;$x<strlen($c);$x++){$y=$c[$x];$v=$t[$f];$u=$l[$f];if($y=='v')$v-=1;if($y=='^')$v+=1;$t[$f]=$v<0?$u:($v>$u?0:$v);$f=($y=='>')?1:($y=='<'?0:$f);}for($z=0;$z<2;$z++)$t[$z]=$t[$z]<9?'0'.$t[$z]:$t[$z];return join(':',$t);
- }
- function _codegolf47_karl4($c) {
- $f = 0;
- $t = [0, 0];
- $l = [23, 59];
- for ($x = 0; $x < strlen($c); $x++) {
- $y = $c[$x];
- $v = $t[$f];
- $u = $l[$f];
- if ($y == 'v') {
- $v -= 1;
- }
- if ($y == '^') {
- $v += 1;
- }
- $t[$f] = $v < 0 ? $u : ($v > $u ? 0 : $v);
- $f = ($y == '>') ? 1 : ($y == '<' ? 0 : $f);
- }
- for ($z = 0; $z < 2; $z++) {
- $t[$z] = $t[$z] < 9 ? '0'.$t[$z] : $t[$z];
- }
- return join(':', $t);
- }
- function codegolf47_mark_poly($c){
- for($i=$h=$m=$p=0;($t=$c[$i++]);)if($t>'u')if($p)$m=($m+59)%60;else $h=($h+23)%24;else if($t>']')if($p)$m=($m+1)%60;else $h=($h+1)%24;else $p=$t>'=';$a=$h<10?'0':'';$b=$m<10?'0':'';return'\0'>"\0"?"$a$h:$b$m":$a+$h+':'+$b+$m;
- }
- function codegolf47_andrew_poly($n) {
- for($t=$h=$m=$i=0;$n[$i]!=null;$i++,$n[$i]=='<'?$t=0:$n[$i]=='>'?$t=1:0){$n[$i]=='^'?$t?($m=$m==59?0:$m+1):$h=$h==23?0:$h+1:0;$n[$i]=='v'?$t?($m=$m==0?59:$m-1):($h=$h==0?23:$h-1):0;}if('\0'=="\0"){$r=($h>9?$h:'0'+$h)+':'+($m>9?$m:'0'+$m);}else{$h=($h>9?$h:"0$h");$m=($m>9?$m:"0$m");$r="$h:$m";}return $r;
- }
- $tests = [
- [
- 'input'=> ['^^>vvv'],
- 'expect'=> '02:57',
- ],
- [
- 'input'=> ['v>^<^>v'],
- 'expect'=> '00:00',
- ],
- [
- 'input'=> [''],
- 'expect'=> '00:00',
- ],
- [
- 'input'=> ['v>>v'],
- 'expect'=> '23:59',
- ],
- [
- 'input'=> ['<<<<>^'],
- 'expect'=> '00:01',
- ],
- [
- 'input'=> ['vvvvvvvvvvvvvvvvvvvvvvvvv>v'],
- 'expect'=> '23:59',
- ],
- ];
- $timerCount = 1;
- ini_set('ignore_repeated_errors', 1);
- require_once 'codegolf.functions.php';
- $filename = __FILE__;
- $file = file($filename);
- $results = [];
- $results2 = [];
- $prefix = basename($filename, '.php');
- $handicaps[$prefix] = [
- 'tyler' => 1 - 0.263,
- 'andrew' => 1 - 0.022,
- 'tw' => 1 + 0.177,
- 'karl' => 1 + 0.137,
- 'rick' => 1 + 0.313,
- 'mark' => 1 - 0.261,
- ];
- foreach (get_defined_functions()['user'] as $func) {
- if ($prefix.'_' != substr($func, 0, strlen($prefix) + 1)) {
- continue;
- }
- echo "\n$func - ";
- // Test function against each provide data set
- $fails = [];
- $passed = true;
- $time = microtime(1);
- foreach ($tests as $test => $params) {
- /** @var $input */
- /** @var $expect */
- extract($params);
- $actual = @call_user_func_array($func, $input);
- if (($actual) != ($expect)) {
- echo "\nTest $test Failed\n"
- , "Expected: \n".(array_value_dump($expect))."\n"
- , "Actual : \n".(array_value_dump($actual))."\n"
- , "Input : \n".(array_value_dump($input))."\n";
- $passed = false;
- $fails[] = $test;
- }
- }
- $time = microtime(1) - $time;
- echo "\n$func - ".($passed ? 'Passed' : "Failed")."\n";
- $chars = func_measure_body($func, $file, 1);
- // Time function
- if (1 < $timerCount) {
- ob_start();
- $time = microtime(1);
- foreach ($tests as $params) {
- /** @var $input */
- /** @var $expect */
- extract($params);
- for ($i = 0; $i < $timerCount; $i++) {
- @call_user_func_array($func, $input);
- if ((microtime(1) - $time) > 10) {
- break 2;
- }
- }
- }
- $time = microtime(1) - $time;
- $time = $time * ($timerCount / $i);
- ob_end_clean();
- }
- preg_match("/{$prefix}_([a-z]+)/", $func, $matches);
- $handicap = $handicaps[$prefix][$matches[1]] ?? 1;
- $results[$func] = [
- 'passed' => $passed ? 'Yes' : 'no',
- 'chars' => $chars,
- 'score' => round($chars / $handicap),
- 'time * '.$timerCount => round($time, 10),
- 'fails' => join(',', $fails),
- ];
- $results2[$func] = [
- 'passed' => $passed ? 'Yes' : 'no',
- 'score' => round($chars / $handicap),
- 'chars' => $chars,
- 'time * '.$timerCount => round($time, 10),
- 'fails' => join(',', $fails),
- ];
- }
- uasort_allkeys($results);
- echo "\n\n".arrayToTextTable($results, 1);
- uasort_allkeys($results2);
- echo "\n\n".arrayToTextTable($results2, 1);
Add Comment
Please, Sign In to add comment