/challenges/automation/basic/1_fast_count.php

Clasificación / Classification: No resuelto aún Not solved yet [ Resuelto / Solved q:^D ]

How fast you can count the lines? / ¿Cómo de rápido puedes contar las líneas?


Count the number of lines of the following text and send the response in less than 10 seconds / Cuenta el número de líneas del siguiente texto y envía la respuesta en menos de 10 segundos.

invalid access slow usb slow system cloned access invalid usb fast disk invalid usb lost access invalid disk slow link slow port lost link cloned port invalid fast system lost access slow usb lost system cloned disk slow port invalid disk lost usb invalid disk lost usb cloned usb slow access cloned usb cloned disk slow link fast usb invalid disk fast usb link lost disk fast usb cloned system fast disk fast system invalid usb cloned port fast usb lost usb fast access invalid disk system invalid link fast access fast port invalid access invalid access lost disk invalid link lost access lost port invalid disk invalid system port cloned access disk fast port disk fast port cloned slow disk cloned link fast access slow disk invalid usb fast slow system invalid invalid usb cloned port invalid system lost access fast system port fast usb slow disk invalid port slow access lost access fast port invalid usb slow system cloned system invalid port lost disk invalid fast port cloned usb fast link slow usb fast system invalid disk cloned system slow usb cloned system fast port lost link cloned usb fast system invalid system fast access cloned usb lost link fast link usb lost disk invalid access fast access slow disk invalid port slow access invalid system lost disk cloned system slow disk invalid disk cloned invalid access lost disk invalid port cloned usb invalid port usb invalid system lost usb invalid disk fast system cloned fast system disk invalid access cloned usb cloned access slow link lost disk fast port lost access slow port slow usb link invalid system slow slow port disk cloned disk cloned link invalid invalid system cloned usb fast link slow system invalid system fast link cloned port lost disk cloned usb fast disk slow disk cloned port cloned system lost link lost usb cloned port cloned system slow usb cloned system disk invalid link slow port fast usb lost fast system invalid disk slow port lost fast disk system invalid usb fast disk cloned disk invalid system fast disk access slow disk cloned invalid access lost slow usb fast access invalid system cloned access fast link slow access fast disk cloned link lost disk system lost system slow usb fast port invalid access cloned usb slow slow port lost system fast access slow usb lost access cloned access invalid fast port invalid system cloned system usb fast disk lost link invalid link fast system fast system invalid port invalid access slow usb fast link lost usb invalid disk lost access invalid port slow access fast port lost usb invalid port cloned usb fast link lost disk cloned port invalid lost link slow cloned usb fast disk invalid system fast usb cloned disk invalid link lost usb fast port invalid link slow access cloned link system fast access link invalid disk cloned port invalid link lost usb lost system fast fast access lost system lost system cloned port lost port system fast system invalid disk slow access system fast port cloned system slow system lost link invalid system lost access slow disk invalid usb fast disk slow link invalid usb system cloned usb fast lost disk cloned system lost link invalid system invalid usb cloned cloned access lost disk cloned usb invalid invalid system cloned usb cloned disk lost system invalid link cloned port slow system fast link lost disk port lost disk slow access cloned link fast disk cloned slow link invalid usb slow port cloned link fast system port invalid port fast lost system invalid disk slow system cloned port lost link fast system slow usb access cloned system lost port cloned access slow link slow disk link lost disk fast slow access lost disk slow system lost usb lost system usb cloned link lost lost disk fast access cloned disk invalid port cloned link fast access slow port fast access cloned system fast port invalid system slow port invalid link slow disk cloned usb slow access invalid disk slow cloned system lost lost link invalid usb fast disk invalid usb slow access system slow disk fast link invalid disk slow invalid access fast link fast port slow link fast link lost system lost usb link invalid port cloned system cloned usb lost port invalid system fast link fast disk slow access cloned disk invalid link lost system cloned access lost access slow disk lost usb slow disk invalid port fast disk slow system lost link invalid system cloned access fast port slow link lost system cloned port invalid system cloned usb invalid disk port invalid system link fast system slow disk invalid link slow usb lost port system lost link disk lost system usb invalid access fast link fast access slow access invalid port slow link lost system cloned usb access slow access usb invalid usb slow port cloned usb slow disk lost disk slow system cloned disk slow port fast link invalid disk lost port fast access slow usb system invalid access lost usb lost usb slow access fast access link cloned disk lost system cloned link fast lost access slow disk cloned disk fast usb invalid disk cloned usb cloned access fast system lost disk invalid system cloned disk lost system cloned port lost system fast system invalid link slow system usb fast disk slow access cloned usb invalid system lost access invalid port fast link invalid system lost port cloned port usb fast disk cloned port slow port invalid lost system cloned access cloned disk slow fast link lost usb cloned system disk invalid system lost disk fast access invalid usb fast disk slow

Solución / Solution :
Ver código fuente
<?php 
include("../../../core.php");

print 
Website::header(array("title"=>"The Automation Chall - Very Easy"));
print 
Challenges::header();

function 
GenerateRandomText($max_lines)
{
    
$words_1     = array("fast","slow","invalid","lost","cloned");
    
$words_2     = array("usb","port","disk","access","system","link");
    
$text "";

    for(
$i=0;$i<$max_lines;$i++)
    {
        
$line="";
        if(
strlen($text)) $text.="\n";
        for(
$j=0;$j<2;$j++)
        {
            
$word_1 $words_1[rand(0,count($words_1)-1)];
            if(!
strstr($line,$word_1))
            {
                
$line  .= $word_1;
                
$line  .= " ";
            }
            
$word_2 $words_2[rand(0,count($words_2)-1)];
            if(!
strstr($line,$word_2))
            {
                
$line  .= $word_2;
                
$line  .= " ";
            }
        }
        
$text.=$line;
    }
    return 
$text;
}

$max_seconds 10;
$max_lines   rand(100,500);

if( !isset(
$_SESSION["time_fc_start"]) ||
    !isset(
$_SESSION["text_fc"]))
{
    
$_SESSION["time_fc_start"] = time();
    
$_SESSION["text_fc"]       = GenerateRandomText($max_lines);
}

$text      $_SESSION["text_fc"];
$num_lines count(preg_split("/\n/",$text));
$elapsed   time()-$_SESSION["time_fc_start"];

if(
$elapsed>$max_seconds)
{
    unset(
$_SESSION["time_fc_start"]);
    if(
Common::getPost("solution")==$num_lines)
    {
        print 
"ELAPSED : ".$elapsed." seconds".PHP_EOL;
        die ( 
"too late, sorry... :(" );
    }
}

?>
<h2>How fast you can count the lines? / ¿Cómo de rápido puedes contar las líneas?</h2>
<br>
Count the number of lines of the following text and 
send the response in less than <b><?=$max_seconds?> seconds</b> 

Cuenta el número de líneas del siguiente texto y envía la respuesta en menos de <b><?=$max_seconds?> segundos</b>. 
<?php
print "<br><br>".PHP_EOL;
print 
"<div class=terminal style='white-space:pre' id=lines>";
print 
$text;
print 
"</div>";
print 
"<a name=sc></a>".PHP_EOL;
print 
"<br>";
print 
"<div id=solution>";
print 
Challenges::solutionBox();
print 
Challenges::checkSolution($num_lines);
print 
"</div>";
?>
<script>
function youlost(){
    window.scrollTo(0,document.body.scrollHeight);
    alert("Sorry, but you lost! / Se más rápido la próxima vez :^)")
    document.getElementById("lines").style.display="none";
    document.getElementById("solution").style.display="none";
    if(document.getElementById("sourcecode"))
        document.getElementById("sourcecode").style.display="none";
}
setTimeout('youlost()',1000*<?=$max_seconds?>);
</script>
<a href="<?=$_SERVER["PHP_SELF"]?>?showSource#sc">Ver código fuente</a>
<?php
if(Common::getString("showSource")!==false) {
    print 
"<div class=sourcecode id=sourcecode>".PHP_EOL;
    
highlight_file(__FILE__);
    print 
"</div>".PHP_EOL;
}else{
    print 
"<script>window.scrollTo(0,0);</script>".PHP_EOL;
}
print 
Website::footer();
?>