Guest User

Untitled

a guest
Jul 20th, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.48 KB | None | 0 0
  1. <?php
  2. /**
  3. * Fill the below function
  4. * You'll be scored on your function's character count of the line(s) between the brackets
  5. * Bragging rights for lowest run time of the line(s) between the brackets
  6. * You cannot use any existing library that solves the heart of the challenge
  7. * You cannot read from your own source code
  8. * All code must appear between the braces of your main function
  9. */
  10. /**
  11. * Given a 24 hour clock set at 00:00 represented as two inputs: hours and minutes
  12. * Given a string of {^,v,<,>}
  13. * - The up arrow (^) will increase the currently active input.
  14. * - The down arrow (v) will decrease the active input.
  15. * - The right arrow (>) will move focus to the right input.
  16. * - The left arrow (<) will move focus to the left input.
  17. * - Up and down movement will loop around as expected for a time input.
  18. * - Left and right movement don't loop around.
  19. * The clock starts out at 00:00 with the hour input active (see first schematic).
  20. * Given a list of input commands, output the resulting time in HH:mm format.
  21. *
  22. * @param string $c Input commands
  23. *
  24. * @return string Resulting time
  25. */
  26. function codegolf47_fake($c) {
  27. return $GLOBALS['expect'];
  28. }
  29.  
  30. function _codegolf47_tyler($c) {
  31. $r=[$p=0,$i=0];
  32. for(;$c[$i];$i++)
  33. switch($c[$i]){
  34. case'>':$p=1;break;
  35. case'<':$p=0;break;
  36. case'^':$r[$p]=$r[$p]++;break;
  37. case'v':$r[$p]=$r[$p]--;break;
  38. }
  39. while(0>$r[0]+=24);
  40. while(0>$r[1]+=60);
  41. return($r[0]%24).':'.($r[1]%60);
  42. }
  43.  
  44. function _codegolf47_tyler2($c) {
  45. $r=[$p=0,$i=0];
  46. for(;$C=$c[$i];$i++)
  47. if('>'==$C)$p=1;
  48. else if('<'==$C)$p=0;
  49. else if('^'==$C)$r[$p]++;
  50. else if('v'==$C)$r[$p]--;
  51. while(0>$r[0]+=24);
  52. while(0>$r[1]+=60);
  53. return($r[0]%24).':'.($r[1]%60);
  54. }
  55. function codegolf47_tyler_poly($c) {
  56. 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);
  57. }
  58.  
  59.  
  60. function codegolf47_tyler_poly3_coop($c) {
  61. 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);
  62. }
  63.  
  64. function _codegolf47_tyler_poly3($c) {
  65. for ($r = [$p = 0, $i = 0]; $C = $c[$i++];) {
  66. if ('>' == $C) {
  67. $p = 1;
  68. } else {
  69. if ('<' == $C) {
  70. $p = 0;
  71. } else {
  72. if ('^' == $C) {
  73. $r[$p]++;
  74. } else {
  75. if ('v' == $C) {
  76. $r[$p]--;
  77. }
  78. }
  79. }
  80. }
  81. }
  82. $R = [($h = (96 + $r[0]) % 24) < 9 ? 0 : '', $h, ':', ($m = (60 + $r[1]) % 60) < 9 ? 0 : '', $m];
  83.  
  84. return '\0' == "\0" ? $R.join('') : join('', $R);
  85. }
  86.  
  87.  
  88. function codegolf47_tyler_php($c) {
  89. 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);
  90. }
  91.  
  92. function _codegolf47_tw2($c) {
  93. $b = 0;
  94. $l = 0;
  95. @$p = str_replace;
  96. $a = explode(" ", $c);
  97. $a = $p('^', 0, $a);
  98. $a = $p('v', 1, $a);
  99. $a = $p('<', 2, $a);
  100. $a = $p('>', 3, $a);
  101. $t = 0;
  102. foreach ($a as $k) {
  103. if ($k == 2) {
  104. $t = 0;
  105. }
  106. if ($k == 3) {
  107. $t = 1;
  108. }
  109. if ($t && $k == 0) {
  110. $b = $b + 1;
  111. }
  112. if ($t == 0 && $k == 1) {
  113. if ($b == 0) {
  114. $b = 23;
  115. }
  116. $b = $b - 1;
  117. }
  118. if ($t == 1 && $k == 0) {
  119. $l = $l + 1;
  120. }
  121. if ($t && $k == 1) {
  122. if ($l == 0) {
  123. $l = 60;
  124. }
  125. $l = $l - 1;
  126. }
  127. }
  128.  
  129. return '0'.$b.':'.'0'.$l;
  130. }
  131.  
  132. function codegolf47_karl4($c) {
  133. $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);
  134. }
  135.  
  136. function _codegolf47_karl4($c) {
  137. $f = 0;
  138. $t = [0, 0];
  139. $l = [23, 59];
  140. for ($x = 0; $x < strlen($c); $x++) {
  141. $y = $c[$x];
  142. $v = $t[$f];
  143. $u = $l[$f];
  144. if ($y == 'v') {
  145. $v -= 1;
  146. }
  147. if ($y == '^') {
  148. $v += 1;
  149. }
  150. $t[$f] = $v < 0 ? $u : ($v > $u ? 0 : $v);
  151. $f = ($y == '>') ? 1 : ($y == '<' ? 0 : $f);
  152. }
  153. for ($z = 0; $z < 2; $z++) {
  154. $t[$z] = $t[$z] < 9 ? '0'.$t[$z] : $t[$z];
  155. }
  156.  
  157. return join(':', $t);
  158. }
  159.  
  160.  
  161.  
  162. function codegolf47_mark_poly($c){
  163. 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;
  164. }
  165.  
  166. function codegolf47_andrew_poly($n) {
  167. 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;
  168. }
  169.  
  170.  
  171. $tests = [
  172. [
  173. 'input'=> ['^^>vvv'],
  174. 'expect'=> '02:57',
  175. ],
  176. [
  177. 'input'=> ['v>^<^>v'],
  178. 'expect'=> '00:00',
  179. ],
  180. [
  181. 'input'=> [''],
  182. 'expect'=> '00:00',
  183. ],
  184. [
  185. 'input'=> ['v>>v'],
  186. 'expect'=> '23:59',
  187. ],
  188. [
  189. 'input'=> ['<<<<>^'],
  190. 'expect'=> '00:01',
  191. ],
  192. [
  193. 'input'=> ['vvvvvvvvvvvvvvvvvvvvvvvvv>v'],
  194. 'expect'=> '23:59',
  195. ],
  196. ];
  197.  
  198.  
  199.  
  200. $timerCount = 1;
  201. ini_set('ignore_repeated_errors', 1);
  202. require_once 'codegolf.functions.php';
  203. $filename = __FILE__;
  204. $file = file($filename);
  205. $results = [];
  206. $results2 = [];
  207. $prefix = basename($filename, '.php');
  208.  
  209. $handicaps[$prefix] = [
  210. 'tyler' => 1 - 0.263,
  211. 'andrew' => 1 - 0.022,
  212. 'tw' => 1 + 0.177,
  213. 'karl' => 1 + 0.137,
  214. 'rick' => 1 + 0.313,
  215. 'mark' => 1 - 0.261,
  216. ];
  217.  
  218. foreach (get_defined_functions()['user'] as $func) {
  219. if ($prefix.'_' != substr($func, 0, strlen($prefix) + 1)) {
  220. continue;
  221. }
  222. echo "\n$func - ";
  223.  
  224. // Test function against each provide data set
  225. $fails = [];
  226. $passed = true;
  227. $time = microtime(1);
  228. foreach ($tests as $test => $params) {
  229. /** @var $input */
  230. /** @var $expect */
  231. extract($params);
  232.  
  233. $actual = @call_user_func_array($func, $input);
  234. if (($actual) != ($expect)) {
  235. echo "\nTest $test Failed\n"
  236. , "Expected: \n".(array_value_dump($expect))."\n"
  237. , "Actual : \n".(array_value_dump($actual))."\n"
  238. , "Input : \n".(array_value_dump($input))."\n";
  239. $passed = false;
  240. $fails[] = $test;
  241. }
  242. }
  243. $time = microtime(1) - $time;
  244. echo "\n$func - ".($passed ? 'Passed' : "Failed")."\n";
  245. $chars = func_measure_body($func, $file, 1);
  246.  
  247. // Time function
  248. if (1 < $timerCount) {
  249. ob_start();
  250. $time = microtime(1);
  251. foreach ($tests as $params) {
  252. /** @var $input */
  253. /** @var $expect */
  254. extract($params);
  255. for ($i = 0; $i < $timerCount; $i++) {
  256. @call_user_func_array($func, $input);
  257.  
  258. if ((microtime(1) - $time) > 10) {
  259. break 2;
  260. }
  261. }
  262. }
  263. $time = microtime(1) - $time;
  264. $time = $time * ($timerCount / $i);
  265. ob_end_clean();
  266. }
  267.  
  268. preg_match("/{$prefix}_([a-z]+)/", $func, $matches);
  269. $handicap = $handicaps[$prefix][$matches[1]] ?? 1;
  270. $results[$func] = [
  271. 'passed' => $passed ? 'Yes' : 'no',
  272. 'chars' => $chars,
  273. 'score' => round($chars / $handicap),
  274. 'time * '.$timerCount => round($time, 10),
  275. 'fails' => join(',', $fails),
  276. ];
  277. $results2[$func] = [
  278. 'passed' => $passed ? 'Yes' : 'no',
  279. 'score' => round($chars / $handicap),
  280. 'chars' => $chars,
  281. 'time * '.$timerCount => round($time, 10),
  282. 'fails' => join(',', $fails),
  283. ];
  284. }
  285. uasort_allkeys($results);
  286. echo "\n\n".arrayToTextTable($results, 1);
  287. uasort_allkeys($results2);
  288. echo "\n\n".arrayToTextTable($results2, 1);
Add Comment
Please, Sign In to add comment