-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle
66 lines (62 loc) · 2.41 KB
/
puzzle
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
<?php
// This code is the answer for a PHP Puzzle by Julian Morley presented by LSquared.
// The code is designed to take input provided by LSquared, identify squared brackets within each string (s) and parse the
// bracketted strings (bs). If 'baab' formated substring of the (bs)->(bss) is found, the entire (s) is invalid; move to
// the next (s). If (bss) does not contain 'baab', prosess the outter-bracket string (obs). If the (obs) contains a 'baab'
// string the (s) is valid and will increass the tracking counter ($r). If the (obs) does not contain a 'baab' the (s) is
// invalid and the next (s) will be processed.
// Initial Variables
$i = './input.txt'; //data being parsed; curl https://dev2.lsquared.com/dev-lsquared-hub/storage/input.1.txt .
$o = './result.txt'; //output result from code to file
$r = 0; //counter
$z = "continue 1";
$y = "continue 2";
$x = "continue 4";
// Code execution
if(file_exists($i)){
$a = file($i);
foreach ($a as $b) {
// Process (s) and create array using '[' and ']' as delimiters
$c = preg_split('/\[([^]]+)\]/', $b, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
// Prossess (bs)
for ($d=1; $d <= count($c) ; $d+=2) {
if(isset($c[$d])){
$e = $c[$d];
}else {
//Process next (bs)
$z;
}
// Process (bss)
for ($f=0; $f <= strlen($e)-4 ; $f++) {
$g = substr($e, $f, 4);
$h=str_split($g, 2);
if ($h[0] == strrev($h[1])) { // If (bs) contains 'baab' process new (s)
$y;
} elseif ($h[0] != strrev($h[1]) && $f < strlen($e)-4) { // If (bs) does not contains 'baab' and not end of (bs) continue process
$z;
} elseif ($f == strlen($e)-4 && $h[0] != $h[1]) { // If entire (bs) processed and no 'baab' found process (obs)
for ($j=0; $j <= count($c); $j+=2) {
if (isset($c[$j])){
$k = $c[$j];
}else{
$y;
}
for ($l=0; $l <= strlen($k)-4 ; $l++) {
$m = substr($k, $l, 4);
$n = str_split($m, 2);
if ($n[0] == strrev($n[1])) {
$r++;
$x;
}
}
}
}
}
}
}
}
$valid = "The valid number of strings in $i is $r."; // Build CLI output
$date = date('c'); // Build TimeStamp
$log = $date . " - " . $valid; // Build log string
echo $valid; // Terminal Output
file_put_contents("./result.log", $log, FILE_APPEND); // Output to .log file