/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.

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

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