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