/challenges/automation/basic/1_fast_count.php

Clasificación / Classification: Muy difícil / Very Hard [ Sin Resolver / Unsolved ]

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 disk fast access slow port fast lost disk cloned port cloned system slow access invalid system slow access invalid link slow disk lost disk usb invalid port link lost system invalid usb fast access slow system fast port cloned link cloned port invalid access fast access invalid disk fast port cloned system slow disk fast link slow system lost access system fast usb invalid port invalid disk fast lost access cloned system fast link slow system invalid disk fast fast disk lost usb slow link cloned usb invalid system cloned cloned disk port lost link disk invalid access slow invalid access slow link cloned disk slow usb lost usb fast cloned usb slow disk lost usb fast access invalid usb cloned port fast access cloned fast usb access invalid access lost disk cloned usb invalid system fast system cloned usb lost usb invalid access slow system fast cloned usb fast link invalid link access cloned access slow fast link usb cloned access usb slow system fast link cloned access invalid port slow access cloned usb lost usb cloned port fast disk cloned usb cloned usb invalid fast access usb fast link disk lost usb invalid access invalid link lost invalid system cloned port cloned link slow slow port cloned slow access disk fast disk invalid invalid disk cloned usb invalid port lost usb slow disk cloned system fast link usb lost disk cloned link lost link fast invalid disk slow access cloned system fast disk fast link invalid access fast port lost lost access fast system invalid usb cloned port invalid system fast usb lost disk fast usb cloned disk lost system fast system port invalid access cloned system lost access fast disk fast port slow access slow usb lost link cloned link fast disk slow usb invalid invalid port cloned system fast link cloned system invalid link slow port invalid disk lost system invalid link slow cloned usb lost system cloned system slow port cloned link slow disk fast port fast usb cloned access fast port cloned access lost system fast port lost disk slow access invalid link access lost system access lost usb fast system slow usb fast disk slow link fast port slow usb lost lost link slow disk invalid access fast link lost access invalid disk slow port fast link slow usb lost system slow system fast access lost port usb slow access fast usb invalid disk link fast access cloned system fast disk lost port slow link slow link lost access lost system fast port fast link slow port invalid disk slow usb cloned link fast usb invalid link cloned access slow link cloned slow usb cloned access fast access slow slow disk cloned slow port lost link lost usb fast system cloned usb disk cloned access port lost link slow fast usb invalid lost system usb fast system invalid disk fast usb slow system fast disk invalid usb invalid disk fast disk lost system slow link invalid disk fast usb invalid link lost usb cloned system cloned disk fast link invalid access lost cloned port lost disk invalid usb cloned access invalid access slow system invalid link fast system cloned access invalid lost access slow cloned disk invalid fast usb cloned slow access invalid link lost disk invalid port cloned link disk lost usb slow access lost system fast disk slow port cloned lost disk invalid system invalid access cloned system fast disk lost system lost port cloned access fast link system cloned port slow system fast disk lost usb disk invalid port slow link lost usb system invalid disk port lost disk system slow link invalid port fast link cloned port slow usb lost link fast disk invalid invalid system slow port cloned usb fast port invalid access usb cloned port fast disk invalid port slow disk invalid access slow port invalid disk lost link cloned port invalid port disk slow access lost system fast port cloned usb invalid link slow access slow system cloned access fast access disk cloned system disk lost system port fast usb lost disk lost disk fast link lost usb fast port fast disk cloned fast system cloned port slow usb lost port cloned system lost disk invalid port lost access slow port cloned usb lost usb cloned disk slow link invalid port slow disk lost fast access lost lost access slow disk fast system lost system slow usb fast usb system fast usb disk fast access lost disk fast port cloned cloned access lost fast access slow disk cloned port fast system fast disk lost usb invalid link cloned access fast link usb lost system invalid fast port cloned link cloned system lost usb slow system invalid port fast access cloned usb fast system disk lost usb fast access fast port lost system fast port slow usb invalid port fast disk cloned system invalid port invalid usb access lost port system cloned link slow access cloned port slow usb invalid link lost access fast usb lost access slow system fast port invalid access usb lost link disk fast disk slow usb slow disk lost port lost link slow port cloned usb lost fast disk lost fast link lost access invalid port fast port lost lost link slow slow usb fast access slow usb invalid usb fast access invalid link fast access lost system fast port cloned system invalid link cloned port fast access fast access invalid lost system slow link cloned system fast usb slow access cloned disk fast system fast port cloned usb fast disk cloned link lost port invalid link lost usb invalid disk lost usb slow link fast usb lost fast usb lost invalid system lost access fast system lost usb slow access fast link lost access lost port cloned disk slow port invalid usb access fast usb lost access cloned disk fast link slow system cloned disk slow link usb invalid usb lost port slow access lost cloned link port fast port cloned invalid access lost port fast link port lost usb fast usb slow access slow link cloned access invalid disk fast invalid usb lost disk slow system lost slow disk fast invalid port lost slow access disk lost access fast link invalid link usb slow access invalid system invalid disk slow link fast link lost disk slow system cloned cloned access port fast port invalid system cloned access invalid invalid link lost system

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();
?>