|
| 1 | +<?php |
| 2 | +header('Content-Type: text/plain; charset=utf-8'); |
| 3 | + |
| 4 | +// https://www.codeeval.com/open_challenges/137/ |
| 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 | + /* |
| 15 | + Dotted decimal 192.0.2.235 with no leading zero. |
| 16 | + Decimal 3221226219 The 32-bit number expressed in decimal. |
| 17 | + |
| 18 | + Dotted hexadecimal 0xc0.0x0.0x02.0xeb Each octet is individually converted to hexadecimal form. |
| 19 | + Hexadecimal 0xC0 00 02 EB Concatenation of the octets from the dotted hexadecimal. |
| 20 | + |
| 21 | + Dotted octal 0300.0000.0002.0353 Each octet is individually converted into octal. |
| 22 | + Octal 030000001353 |
| 23 | + |
| 24 | + Dotted binary 11000000.00000000.00000010.11101011 Each octet is individually converted into binary. |
| 25 | + Binary 11000000 00000000 00000010 11101011 |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + // 1.0.0.0 => 255.255.255.254 |
| 30 | + // 1-255 => (25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9]) |
| 31 | + // 0-255 => (25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]) |
| 32 | + // 0-254 => (25[0-4]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]) |
| 33 | + */ |
| 34 | + |
| 35 | + $dotted_decimal_pattern = '(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])'; |
| 36 | + $dotted_decimal_pattern .= '\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])'; |
| 37 | + $dotted_decimal_pattern .= '\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])'; |
| 38 | + $dotted_decimal_pattern .= '\.(25[0-4]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])'; |
| 39 | + $dotted_decimal_pattern = '/'.$dotted_decimal_pattern.'/'; |
| 40 | + // echo $dotted_decimal_pattern."\n"; |
| 41 | + |
| 42 | + // 16777216 => 4 294 967 294 |
| 43 | + $decimal_pattern = '/('; |
| 44 | + $decimal_pattern .= '429496729[0-4]'; |
| 45 | + $decimal_pattern .= '|42949672[0-8][0-9]'; |
| 46 | + $decimal_pattern .= '|4294967[0-2][0-9]{2}'; |
| 47 | + $decimal_pattern .= '|429496[0-7][0-9]{3}'; |
| 48 | + $decimal_pattern .= '|42949[0-6][0-9]{4}'; |
| 49 | + $decimal_pattern .= '|4294[0-8][0-9]{5}'; |
| 50 | + $decimal_pattern .= '|429[0-3][0-9]{6}'; |
| 51 | + $decimal_pattern .= '|42[0-8][0-9]{7}'; |
| 52 | + $decimal_pattern .= '|4[0-1][0-9]{8}'; |
| 53 | + |
| 54 | + $decimal_pattern .= '|[1-9][0-9]{8}'; |
| 55 | + |
| 56 | + $decimal_pattern .= '|1677721[6-9]'; |
| 57 | + $decimal_pattern .= '|167772[2-9][0-9]'; |
| 58 | + $decimal_pattern .= '|16777[3-9][0-9]{2}'; |
| 59 | + $decimal_pattern .= '|1677[8-9][0-9]{3}'; |
| 60 | + $decimal_pattern .= '|167[8-9][0-9]{4}'; |
| 61 | + $decimal_pattern .= '|16[8-9][0-9]{5}'; |
| 62 | + $decimal_pattern .= '|1[7-9][0-9]{6}'; |
| 63 | + $decimal_pattern .= '|[2-9][0-9]{7}'; |
| 64 | + $decimal_pattern .= ')/'; |
| 65 | + // echo $decimal_pattern ."\n"; |
| 66 | + |
| 67 | + // 0xc0.0x0.0x02.0xeb |
| 68 | + // 0x01.0x0.0x0.0x0 => 0xff.0xff.0xff.0xfe |
| 69 | + $dotted_hexa_pattern = '(0x[1-f][0-f]|0x0[1-f]|0x[1-f])'; // 0x01=>0xff |
| 70 | + $dotted_hexa_pattern .= '\.(0x[0-f]{2}|0x0)'; // 0x0=>0xff |
| 71 | + $dotted_hexa_pattern .= '\.(0x[0-f]{2}|0x0)'; // 0x0=>0xff |
| 72 | + $dotted_hexa_pattern .= '\.(0xf[0-e]|0x[1-e][0-f]|0x0[0-f]|0x0)'; // 0x0=>0xfe |
| 73 | + $dotted_hexa_pattern = '/'.$dotted_hexa_pattern.'/'; |
| 74 | + // echo $dotted_hexa_pattern."\n"; |
| 75 | + |
| 76 | + $hexa_pattern = str_replace('\.','',$dotted_hexa_pattern); |
| 77 | + $hexa_pattern = str_replace('/','',$hexa_pattern); |
| 78 | + $hexa_pattern = '/0x' . str_replace('0x','',$hexa_pattern).'/'; |
| 79 | + // echo $hexa_pattern."\n"; |
| 80 | + |
| 81 | + // 0001.0000.0000.000 => 0377.0377.0377.0376 |
| 82 | + $dotted_octal_pattern = '(0[1-3][0-7]{2}|00[1-7][0-7]|000[1-7])'; // 0001=>0377 |
| 83 | + $dotted_octal_pattern .= '\.(0[1-3][0-7]{2}|00[0-7]{2})'; // 0000=>0377 |
| 84 | + $dotted_octal_pattern .= '\.(0[1-3][0-7]{2}|00[0-7]{2})'; // 0000=>0377 |
| 85 | + $dotted_octal_pattern .= '\.(037[0-6]|03[0-6][0-7]|0[1-2][0-7]|00[0-7]{2})'; // 0000=>0376 |
| 86 | + $dotted_octal_pattern = '/'.$dotted_octal_pattern.'/'; |
| 87 | + // echo $dotted_octal_pattern."\n"; |
| 88 | + |
| 89 | + // 1 00 00 00 00 => 3 77 77 77 77 76 |
| 90 | + |
| 91 | + $octal_pattern = '/('; |
| 92 | + $octal_pattern .= '3777777777[0-6]'; |
| 93 | + $octal_pattern .= '|377777777[0-6][0-7]'; |
| 94 | + $octal_pattern .= '|37777777[0-6][0-7]{2}'; |
| 95 | + $octal_pattern .= '|3777777[0-6][0-7]{3}'; |
| 96 | + $octal_pattern .= '|377777[0-6][0-7]{4}'; |
| 97 | + $octal_pattern .= '|37777[0-6][0-7]{5}'; |
| 98 | + $octal_pattern .= '|3777[0-6][0-7]{6}'; |
| 99 | + $octal_pattern .= '|377[0-6][0-7]{7}'; |
| 100 | + $octal_pattern .= '|37[0-6][0-7]{8}'; |
| 101 | + $octal_pattern .= '|3[0-6][0-7]{9}'; |
| 102 | + $octal_pattern .= '|[1-2][0-7]{10}'; |
| 103 | + $octal_pattern .= '|[1-7][0-7]{8}'; |
| 104 | + $octal_pattern .= ')/'; |
| 105 | + // echo $octal_pattern."\n"; |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + // 00000001.00000000.00000000.00000000 => 11111111.11111111.11111111.11111110 |
| 111 | + $dotted_binary_pattern = '([0-1]{7}1|[0-1]{6}1[0-1]|[0-1]{5}1[0-1]{2}|[0-1]{4}1[0-1]{3}|[0-1]{3}1[0-1]{4}|[0-1]{2}1[0-1]{5}|[0-1]1[0-1]{6}|1[0-1]{7})'; // 00000001=>11111111 |
| 112 | + $dotted_binary_pattern .= '\.([0-1]{8})'; // 00000000=>11111111 |
| 113 | + $dotted_binary_pattern .= '\.([0-1]{8})'; // 00000000=>11111111 |
| 114 | + $dotted_binary_pattern .= '\.([0-1]{7}0|[0-1]{6}0[0-1]|[0-1]{5}0[0-1]{2}|[0-1]{4}0[0-1]{3}|[0-1]{3}0[0-1]{4}|[0-1]{2}0[0-1]{5}|[0-1]0[0-1]{6}|0[0-1]{7})'; // 00000000=>11111110 |
| 115 | + $dotted_binary_pattern = '/'.$dotted_binary_pattern.'/'; |
| 116 | + // echo $dotted_binary_pattern."\n"; |
| 117 | + |
| 118 | + $binary_pattern = str_replace('\.','',$dotted_binary_pattern); |
| 119 | + // echo $binary_pattern."\n"; |
| 120 | + |
| 121 | + $patterns = array( |
| 122 | + 'dotted_hexa_pattern' => $dotted_hexa_pattern, |
| 123 | + 'dotted_binary_pattern' => $dotted_binary_pattern, |
| 124 | + 'dotted_octal_pattern' => $dotted_octal_pattern, |
| 125 | + 'dotted_decimal_pattern' => $dotted_decimal_pattern, |
| 126 | + 'hexa_pattern' => $hexa_pattern, |
| 127 | + 'binary_pattern' => $binary_pattern, // invert order? |
| 128 | + 'octal_pattern' => $octal_pattern, // invert order? |
| 129 | + 'decimal_pattern' => $decimal_pattern, |
| 130 | + ); |
| 131 | + |
| 132 | + // print_r($patterns); |
| 133 | + |
| 134 | + $ips = array(); |
| 135 | + |
| 136 | + while ( $fp && !feof( $fp ) ) { |
| 137 | + $line = trim(fgets($fp)); |
| 138 | + if( $line ){ |
| 139 | + foreach($patterns as $name => $pattern){ |
| 140 | + // echo $name.': '."\n"; |
| 141 | + if( preg_match_all($pattern, $line, $matches_sets, PREG_SET_ORDER ) ){ |
| 142 | + for($i=0, $cnt = count($matches_sets) ; $i < $cnt ; $i++){ |
| 143 | + $matches = $matches_sets[$i]; |
| 144 | + $ip = false; |
| 145 | + switch( $name ){ |
| 146 | + case 'dotted_hexa_pattern' : |
| 147 | + $ip = hexdec($matches[1]).'.'.hexdec($matches[2]).'.'.hexdec($matches[3]).'.'.hexdec($matches[4]); |
| 148 | + break; |
| 149 | + case 'dotted_binary_pattern' : |
| 150 | + $ip = bindec($matches[1]).'.'.bindec($matches[2]).'.'.bindec($matches[3]).'.'.bindec($matches[4]); |
| 151 | + break; |
| 152 | + case 'dotted_octal_pattern' : |
| 153 | + $ip = octdec($matches[1]).'.'.octdec($matches[2]).'.'.octdec($matches[3]).'.'.octdec($matches[4]); |
| 154 | + break; |
| 155 | + case 'dotted_decimal_pattern' : |
| 156 | + $ip = $matches[1].'.'.$matches[2].'.'.$matches[3].'.'.$matches[4]; |
| 157 | + break; |
| 158 | + case 'hexa_pattern' : |
| 159 | + $ip = hexdec($matches[1]).'.'.hexdec($matches[2]).'.'.hexdec($matches[3]).'.'.hexdec($matches[4]); |
| 160 | + break; |
| 161 | + case 'binary_pattern' : |
| 162 | + $ip = bindec($matches[1]).'.'.bindec($matches[2]).'.'.bindec($matches[3]).'.'.bindec($matches[4]); |
| 163 | + break; |
| 164 | + case 'octal_pattern' : |
| 165 | + $bin = sprintf('%32b',octdec($matches[1])); |
| 166 | + $bin = str_split($bin,8); |
| 167 | + $ip = bindec($bin[0]).'.'.bindec($bin[1]).'.'.bindec($bin[2]).'.'.bindec($bin[3]); |
| 168 | + break; |
| 169 | + case 'decimal_pattern' : |
| 170 | + $bin = sprintf('%32b',$matches[1]); |
| 171 | + $bin = str_split($bin,8); |
| 172 | + $ip = bindec($bin[0]).'.'.bindec($bin[1]).'.'.bindec($bin[2]).'.'.bindec($bin[3]); |
| 173 | + break; |
| 174 | + } |
| 175 | + if( $ip ){ |
| 176 | + if( !isset($ips[$ip]) ){ |
| 177 | + $ips[$ip] = 0; |
| 178 | + } |
| 179 | + $ips[$ip]++; |
| 180 | + } |
| 181 | + } |
| 182 | + }// |
| 183 | + } |
| 184 | + }// |
| 185 | + }// |
| 186 | + fclose( $fp ); |
| 187 | + arsort($ips); |
| 188 | + list($k,$v) = each($ips); |
| 189 | + $ips = array_keys($ips, $v); |
| 190 | + sort($ips); |
| 191 | + echo implode(' ',$ips)."\n"; |
| 192 | + } |
| 193 | + else{ |
| 194 | + echo '!fp'."\n"; |
| 195 | + } |
| 196 | + } |
| 197 | + else{ |
| 198 | + echo '!readable'."\n"; |
| 199 | + } |
| 200 | + } |
| 201 | + else{ |
| 202 | + echo '!file_exists'."\n"; |
| 203 | + } |
| 204 | +} |
| 205 | +else{ |
| 206 | + echo '!argv[1]'."\n"; |
| 207 | +} |
| 208 | + |
| 209 | +exit(0); |
| 210 | + |
| 211 | +?> |
0 commit comments