-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp-puzzle-solver.php
129 lines (124 loc) · 3.63 KB
/
php-puzzle-solver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
// https://github.com/sibbu2005/PHP-Puzzle/blob/master/echo.php
$file = fopen("input.txt","r");
/*
*Read file line by line and process it
*/
$supported_staranger_id_count = 0;
while(($line = fgets($file)) !== false) {
$continue = false;
/*
* using regular expression to extract the text inside the square bracket
*/
preg_match_all("/\[(.*?)\]/", $line, $matches);
//loop through the strings inside all the square brackets
if(isset($matches[1]) && !empty($matches[1])){
foreach ($matches[1] as $inner_string) {
switch(strlen($inner_string)<>4){
case 0:
/*
* if length of inner string in bracket is 4 and is of "baab" type
* go to next line and count for this line is zero
*/
if($inner_string[0]!=$inner_string[1] && $inner_string == strrev($inner_string)){
$continue = true;
}
break;
case 1:
/*
* if length of inner string in bracket is greater than 4 and is of "baab" type
* go to next line and count for this line is zero
*/
for($i=0;$i<strlen($inner_string)-4;$i++){
$substr_inner = substr($inner_string,$i,4);
if($substr_inner == strrev($substr_inner) && $substr_inner[0] != $substr_inner[1]){
$continue = true;
break;
}
}
break;
} //end switch
/*
* break the foreach loop if any "baab" type substring occurs inside bracket
* no need to check further
*/
if($continue){
break;
}
} // end foreach
}else{
switch(strlen($line)<>4){
case 0:
/*
* if length of line is 4 and is of "baab" type and has no brackets
* count it as one , this is a match
*/
if($line[0]!=$line[1] && $line == strrev($line)){
$supported_staranger_id_count++;
$continue = true;
}
break;
case 1:
/*
* if length of line is greater than 4 and is of "baab" type and has no brackets
* count it as one , this is a match
*/
for($i=0;$i<strlen($line)-4;$i++){
$substr_inner = substr($line,$i,4);
if($substr_inner == strrev($substr_inner) && $substr_inner[0] != $substr_inner[1]){
$supported_staranger_id_count++;
$continue = true;
break;
}
}
break;
} //end switch
}
if($continue){
/*
* break the foreach loop if any "baab" type substring occurs inside bracket
* no need to check further and go to next line
*/
continue;
}else{
/*
* if sub-strings inside square bracket is not "baab" type
* Check sub-strings outside the bracket , if "baab" type
*/
$outer_matches = preg_split("/\[(.*?)\]/", $line);
foreach ($outer_matches as $outer_string) {
switch(strlen($outer_string)<>4){
case 0:
/*
* if length of outer string is 4 and is of "baab" type
* count it as one , this is a match
*/
if($outer_string[0]!=$outer_string[1] && $outer_string == strrev($outer_string)){
$supported_staranger_id_count++;
$continue = true;
}
break;
case 1:
/*
* if length of outer string is greater than 4 and is of "baab" type
* count it as one , this is a match
*/
for($i=0;$i<strlen($outer_string)-4;$i++){
$substr_inner = substr($outer_string,$i,4);
if($substr_inner == strrev($substr_inner) && $substr_inner[0] != $substr_inner[1]){
$supported_staranger_id_count++;
$continue = true;
break;
}
}
break;
} //end switch
if($continue){
break;
}
} // end foreach
}//end
} // end while
echo "Total Number of Valid StangerID : ".$supported_staranger_id_count;
fclose($file);
?>