Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $input = <<<END
- CARVES
- FOSTER
- COGENT
- SCRIPT
- ATTEND
- ENRAGE
- CALVES
- UNREST
- CAPPED
- LETHAL
- EASTER
- SOCCER
- RENDER
- ESCAPE
- MAPLES
- CRANIA
- SCURRY
- DECENT
- BUGLES
- ENLIST
- RESETS
- VERIFY
- BOUGHT
- ENDEAR
- STYLED
- BARBER
- RAISES
- ESTATE
- TAKERS
- BASKED
- ACCENT
- ADVERT
- TALKIE
- BRUTAL
- STARRY
- TAUTLY
- WEASEL
- AGENDA
- RAIDED
- STRAIT
- REELER
- ESSAYS
- TAPERS
- MASSES
- TASTED
- PRIMAL
- BOOTHS
- GROTTO
- RADIUM
- STADIA
- BARFLY
- INSERT
- OCTAVE
- SERIAL
- SHAVES
- PIERCE
- ANTLER
- SCARES
- STOLEN
- REPUTE
- HARPER
- TASTER
- ANEMIA
- OCTANE
- STILLS
- POSTER
- END;
- function check_match($array){
- $len = $array['len'];
- for($x = 0; $x < $len; $x++){
- for($y = 0; $y < $len; $y++){
- if($array[$x][$y] != $array[$y][$x]){
- return false;
- }
- }
- }
- return true;
- }
- function match_word($array, $word, $len){
- //$len = strlen($word);
- $temp_word = str_split($word);
- $out_ar = array();
- foreach($array as $key => $value){
- if(!in_array($word, $value['hist'])){
- for($x = 0; $x < $len; $x++){
- for($y = 0; $y < $len; $y++){
- if(!isset($value[$x][$y])){
- $continue = true;
- for($x2 = 0; $x2 < $value['len']; $x2++){
- $letter = $word[$x2];
- $letter2 = $value[$x2][$y];
- if($value[$x2][$y] != $temp_word[$x2] && isset($value[$x2][$y])){
- //$y = $len;
- //echo "Breaking Y: $y\n";
- $continue = false;
- break;
- }
- }
- if($continue && !in_array($word, $value['hist'])){
- $temp_ar = $array[$key];
- $temp_ar['hist'][] = $word;
- for($x2 = 0; $x2 < $value['len']; $x2++){
- //echo " Inserting $word into {$value['hist'][0]} $x2\n";
- $temp_ar[$x2][$y] = $temp_word[$x2];
- $temp_ar[$y][$x2] = $temp_word[$x2];
- }
- if(check_match($temp_ar)){
- $array[$key] = $temp_ar; //I would like to make this line $array[] = $temp_ar, but this uses gigabytes of storage.
- }
- }
- }
- }
- }
- }
- }
- return $array;
- }
- function build_array($array){
- foreach($array as $value){
- $temp_str = str_split($value);
- //temp[x][y] = char
- $c = 0;
- foreach($temp_str as $value2){
- $temp[0][$c] = $value2;
- $temp[$c][0] = $value2;
- $c++;
- }
- $temp['hist'][0] = $value;
- $temp['len'] = strlen($value);
- $out[] = $temp;
- }
- return $out;
- }
- function multipleExplode($delimiters = array(), $string = ''){
- if(count($delimiters) == 1){
- return explode($delimiters[0], $string);
- }
- $mainDelim=$delimiters[count($delimiters)-1]; // dernier
- array_pop($delimiters);
- foreach($delimiters as $delimiter){
- $string= str_replace($delimiter, $mainDelim, $string);
- }
- $result = explode($mainDelim, $string);
- return $result;
- }
- $input = multipleExplode(array("\n"), $input);
- $word_table = build_array($input);
- $len = $word_table[0]['len'];
- foreach($input as $word){
- //echo "Current Word: $word\n\n";
- $word_table = match_word($word_table, $word, $len);
- }
- $output = array();
- foreach($word_table as $key => $value){
- //echo "printing...\n";
- $cont = true;
- for($x = 0; $x < $len; $x++){
- for($y = 0; $y < $len; $y++){
- if(!isset($value[$x][$y])){
- //echo " $key $x $y IS EMPTY\n\n";
- $cont = false;
- break 2;
- }
- }
- }
- if($cont && !in_array($value, $output)){
- $output[] = $value;
- }
- }
- foreach($output as $value){
- for($x = 0; $x < $len; $x++){
- for($y = 0; $y < $len; $y++){
- echo $value[$x][$y];
- }
- echo "\n";
- }
- echo "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment