|
| 1 | +<?php |
| 2 | +header('Content-Type: text/plain; charset=utf-8'); |
| 3 | + |
| 4 | +// https://www.codeeval.com/open_challenges/133/ |
| 5 | + |
| 6 | +if( isset($_GET['f_i_l_e']) && $_GET['f_i_l_e'] ) $argv[1] = $_GET['f_i_l_e']; |
| 7 | + |
| 8 | +if( isset($argv[1]) && $argv[1] ){ |
| 9 | + $filename = $argv[1]; |
| 10 | + if( file_exists($filename) ){ |
| 11 | + if( is_readable($filename) ){ |
| 12 | + $fp = fopen($filename, 'r'); |
| 13 | + if( $fp ){ |
| 14 | + while ( $fp && !feof( $fp ) ) { |
| 15 | + $line = trim(fgets($fp)); |
| 16 | + if( $line ){ |
| 17 | + // echo $line."\n"; |
| 18 | + list($streets, $avenues) = explode(' ',$line); |
| 19 | + $streets = explode(',',trim($streets,'()')); |
| 20 | + $avenues = explode(',',trim($avenues,'()')); |
| 21 | + |
| 22 | + $smax = count($streets) - 1; |
| 23 | + $amax = count($avenues) - 1; |
| 24 | + |
| 25 | + $tan = $avenues[$amax] / $streets[$smax]; |
| 26 | + |
| 27 | + $crossed = 0; |
| 28 | + |
| 29 | + // echo count($avenues)."\n"; |
| 30 | + // echo $tan . "\n"; |
| 31 | + for( $s=0 ; $s < $smax ; $s++ ){ |
| 32 | + for( $a=0 ; $a < $amax ; $a++ ){ |
| 33 | + $curr_x = $streets[$s]; |
| 34 | + $curr_y = $avenues[$a]; |
| 35 | + |
| 36 | + $next_x = $streets[$s+1]; |
| 37 | + $next_y = $avenues[$a+1]; |
| 38 | + |
| 39 | + // echo 'from ('.$curr_x.','.$curr_y.') to ('.$next_x.','.$next_y.')'."\n"; |
| 40 | + if ( ($next_x > ($curr_y / $tan)) && ($next_y > ($tan * $curr_x)) ){ |
| 41 | + $crossed++; |
| 42 | + // echo '++'."\n"; |
| 43 | + } |
| 44 | + |
| 45 | + } |
| 46 | + } |
| 47 | + echo $crossed."\n"; |
| 48 | + } |
| 49 | + |
| 50 | + }// |
| 51 | + fclose( $fp ); |
| 52 | + } |
| 53 | + else{ |
| 54 | + echo '!fp'."\n"; |
| 55 | + } |
| 56 | + } |
| 57 | + else{ |
| 58 | + echo '!readable'."\n"; |
| 59 | + } |
| 60 | + } |
| 61 | + else{ |
| 62 | + echo '!file_exists'."\n"; |
| 63 | + } |
| 64 | +} |
| 65 | +else{ |
| 66 | + echo '!argv[1]'."\n"; |
| 67 | +} |
| 68 | + |
| 69 | +exit(0); |
| 70 | + |
| 71 | +?> |
0 commit comments