Solutions

This commit is contained in:
Simon Vieille 2015-03-02 19:42:39 +01:00
commit 0ef619e034
13 changed files with 730 additions and 0 deletions

View File

@ -0,0 +1,38 @@
<?php
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
fscanf(STDIN, "%d",
$L
);
fscanf(STDIN, "%d",
$H
);
$T = stream_get_line(STDIN, 256, "\n");
$T = preg_replace('/\W/', '?', $T);
$lines = [];
$outputLines = [];
for ($i = 0; $i < $H; $i++)
{
$lines[] = stream_get_line(STDIN, 1024, "\n");
$outputLines[] = '';
}
$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ?';
for ($u = 0; $u < strlen($T); $u++) {
$position = strpos($letters, strtoupper($T[$u]));
$offsetLeft = $position * $L;
foreach($lines as $k => $line) {
$outputLines[$k] .= substr($lines[$k], $offsetLeft, $L);
}
}
echo implode("\n", $outputLines), "\n";

View File

@ -0,0 +1,40 @@
<?php
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
fscanf(STDIN, "%d",
$N
);
$pis_ = [];
$pis = [];
for ($i = 0; $i < $N; $i++)
{
fscanf(STDIN, "%d", $Pi);
$pis_[] = $Pi;
}
asort($pis_);
foreach($pis_ as $p) {
$pis[] = $p;
}
$min = $Pi;
for ($u = 0; $u<count($pis)-1; $u++) {
$d = $pis[$u + 1] - $pis[$u];
if ($d < $min) {
$min = $d;
}
}
// Write an action using echo(). DON'T FORGET THE TRAILING \n
// To debug (equivalent to var_dump): error_log(var_export($var, true));
echo("$min\n");
?>

View File

@ -0,0 +1,51 @@
<?php
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
$letters = trim(stream_get_line(STDIN, 100, "\n"));
// Write an action using echo(). DON'T FORGET THE TRAILING \n
// To debug (equivalent to var_dump): error_log(var_export($var, true));
$resultat = [];
$bstring = '';
for ($u = 0; $u < strlen($letters); $u++) {
$binary = decbin(ord((string) $letters[$u]));
$_bstring = (string) $binary;
$_bstring = str_pad($_bstring, 7, '0', STR_PAD_LEFT);
$bstring.= $_bstring;
}
$p = 1;
$c = $bstring[0];
$value = $c == 1 ? '0' : '00';
$counter = 1;
while ($p < strlen($bstring) + 1) {
$nw = $bstring[$p];
if ($nw == $c) {
$counter++;
} else {
$resultat[] = $value.' '.str_repeat('0', $counter);
$c = $nw;
$counter = 1;
if ($value === '00') {
$value = '0';
} else {
$value = '00';
}
}
$p++;
}
echo implode(' ', $resultat), "\n";

View File

@ -0,0 +1,53 @@
<?php
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
fscanf(STDIN, "%s",
$LON
);
fscanf(STDIN, "%s",
$LAT
);
fscanf(STDIN, "%d",
$N
);
$defibs = [];
for ($i = 0; $i < $N; $i++)
{
$defibs[] = explode(';', stream_get_line(STDIN, 256, "\n"));
}
$key = null;
$distance = null;
function f($s) {
return (float) str_replace(',', '.', $s);
}
$la1 = f($LAT);
$lo1 = f($LON);
foreach ($defibs as $k => $defib) {
$la2 = f($defib[5]);
$lo2 = f($defib[4]);
$x = ($lo2 - $lo1) * cos(($la1 + $la2) / 2);
$y = ($la2 - $la1);
$d = sqrt($x*$x + $y*$y) * 6371;
if (null === $distance || $d < $distance) {
$distance = $d;
$key = $k;
}
}
// Write an action using echo(). DON'T FORGET THE TRAILING \n
// To debug (equivalent to var_dump): error_log(var_export($var, true));
echo $defibs[$key][1], "\n";
?>

View File

@ -0,0 +1,51 @@
<?php
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
// game loop
while (TRUE)
{
fscanf(STDIN, "%d %d",
$SX,
$SY
);
$MHs = [];
for ($i = 0; $i < 8; $i++)
{
fscanf(STDIN, "%d",
$MH // represents the height of one mountain, from 9 to 0. Mountain heights are provided from left to right.
);
$MHs[] = $MH;
}
error_log(var_export($MHs, true));
// Write an action using echo(). DON'T FORGET THE TRAILING \n
// To debug (equivalent to var_dump): error_log(var_export($var, true));
$tolder = null;
$key = null;
foreach ($MHs as $k => $v) {
if ($tolder === null) {
$key = $k;
$tolder = $v;
} else {
if ($v > $tolder) {
$key = $k;
$tolder = $v;
}
}
}
if ($SX === $key) {
echo "FIRE\n";
} else {
echo "HOLD\n";
}
}
?>

View File

@ -0,0 +1,104 @@
<?php
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
fscanf(STDIN, "%d %d %d %d",
$LX, // the X position of the light of power
$LY, // the Y position of the light of power
$TX, // Thor's starting X position
$TY // Thor's starting Y position
);
// game loop
while (TRUE)
{
fscanf(STDIN, "%d",
$E // The level of Thor's remaining energy, representing the number of moves he can still make.
);
if ($TX === $LX) {
if ($TY >= $LT) {
echo "N\n";
$TY--;
} else {
$TY++;
echo "S\n";
}
}
if ($TY === $LY) {
if ($TX > $LX) {
echo "W\n";
$TX--;
} else {
echo "E\n";
$TX++;
}
}
if ($TX > $LX && $LY > $TY) {
if (($TX - $LX) > ($LY - $TY)) {
echo "W\n";
$TX--;
}
elseif (($TX - $LX) < ($LY - $TY)) {
echo "S\n";
$TY++;
}
else {
$TY++;
$TX--;
echo "SW\n";
}
}
if ($TX < $LX && $LY > $TY) {
if ($LX - $TX > $LY - $TY) {
echo "E\n";
$TX++;
}
elseif ($LX - $TX < $LY - $TY) {
echo "S\n";
$TY++;
}
else {
echo "SE\n";
$TY++;
$TX++;
}
}
if ($TX > $LX && $LY < $TY) {
if ($TX - $LX > $TY - $LY) {
echo "W\n";
$TX--;
}
elseif ($TX - $LX < $TY - $LY) {
echo "N\n";
$TY--;
}
else {
echo "NW\n";
$TX--;
$TY--;
}
}
if ($TX < $LX && $LY < $TY) {
if ($LX - $TX > $TY - $LY) {
echo "E\n";
$TX++;
}
elseif ($LX - $TX < $TY - $LY) {
echo "N\n";
$TY--;
}
else {
echo "NE\n";
$TX++;
$TY--;
}
}
}

View File

@ -0,0 +1,45 @@
<?php
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
fscanf(STDIN, "%d",
$N // Number of elements which make up the association table.
);
fscanf(STDIN, "%d",
$Q // Number Q of file names to be analyzed.
);
$mimes = [];
$files = [];
for ($i = 0; $i < $N; $i++)
{
fscanf(STDIN, "%s %s",
$EXT, // file extension
$MT // MIME type.
);
$mimes[strtolower($EXT)] = $MT;
}
for ($i = 0; $i < $Q; $i++)
{
$files[] = stream_get_line(STDIN, 500, "\n");
}
foreach ($files as $file) {
$e = explode('.', $file);
if (count($e) < 2) {
echo("UNKNOWN\n");
continue;
}
$ext = strtolower(array_pop($e));
echo isset($mimes[$ext]) ? $mimes[$ext] : 'UNKNOWN', "\n";
}
// Write an action using echo(). DON'T FORGET THE TRAILING \n
// To debug (equivalent to var_dump): error_log(var_export($var, true));

View File

@ -0,0 +1,56 @@
<?php
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
fscanf(STDIN, "%d",
$N // the number of points used to draw the surface of Mars.
);
for ($i = 0; $i < $N; $i++)
{
fscanf(STDIN, "%d %d",
$LAND_X, // X coordinate of a surface point. (0 to 6999)
$LAND_Y // Y coordinate of a surface point. By linking all the points together in a sequential fashion, you form the surface of Mars.
);
}
// game loop
while (TRUE)
{
fscanf(STDIN, "%d %d %d %d %d %d %d",
$X,
$Y,
$HS, // the horizontal speed (in m/s), can be negative.
$VS, // the vertical speed (in m/s), can be negative.
$F, // the quantity of remaining fuel in liters.
$R, // the rotation angle in degrees (-90 to 90).
$P // the thrust power (0 to 4).
);
// Write an action using echo(). DON'T FORGET THE TRAILING \n
// To debug (equivalent to var_dump): error_log(var_export($var, true));
if ($R !== 0) {
if ($R > 0) {
echo -$R, ' 0', "\n";
} else {
echo $R, ' 0', "\n";
}
}
error_log(var_export($VS, true));
if ($Y > 2300) {
echo 0, ' 1', "\n";
}
elseif ($Y > 1900) {
echo 0, ' 2', "\n";
}
else {
if ($VS < -39) {
echo 0, ' 4', "\n";
} else {
echo '0 0', "\n";
}
}
}
?>

View File

@ -0,0 +1,28 @@
<?php
/**
* CodinGame planet is being attacked by slimy insectoid aliens.
**/
$u = 0;
// game loop
while (TRUE)
{
fscanf(STDIN, "%s",
$enemy1 // name of enemy 1
);
fscanf(STDIN, "%d",
$dist1 // distance to enemy 1
);
fscanf(STDIN, "%s",
$enemy2 // name of enemy 2
);
fscanf(STDIN, "%d",
$dist2 // distance to enemy 2
);
// Write an action using echo(). DON'T FORGET THE TRAILING \n
// To debug (equivalent to var_dump): error_log(var_export($var, true));
echo $dist2 < $dist1 ? $enemy2 : $enemy1, "\n";
}
?>

View File

@ -0,0 +1,46 @@
<?php
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
fscanf(STDIN, "%d",
$R // the length of the road before the gap.
);
fscanf(STDIN, "%d",
$G // the length of the gap.
);
fscanf(STDIN, "%d",
$L // the length of the landing platform.
);
// game loop
while (TRUE)
{
fscanf(STDIN, "%d",
$S // the motorbike's speed.
);
fscanf(STDIN, "%d",
$X // the position on the road of the motorbike.
);
// Write an action using echo(). DON'T FORGET THE TRAILING \n
// To debug (equivalent to var_dump): error_log(var_export($var, true));
if ($X + 1 === $R) {
echo("JUMP\n");
}
elseif($G < $S - 1) {
echo("SLOW\n");
}
elseif ($X >= $R + $G) {
echo("SLOW\n");
}
elseif ($G == $S - 1) {
echo("WAIT\n");
}
else {
echo("SPEED\n"); // A single line containing one of 4 keywords: SPEED, SLOW, JUMP, WAIT.
}
}
?>

View File

@ -0,0 +1,46 @@
<?php
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
fscanf(STDIN, "%d",
$N // the number of temperatures to analyse
);
$TEMPS = explode(' ', stream_get_line(STDIN, 256, "\n")); // the N temperatures expressed as integers ranging from -273 to 5526
$key = null;
$diff = null;
if (count($TEMPS) === 1) {
echo "0\n";
}
foreach ($TEMPS as $k => $v) {
$d = abs((int) $v);
if ($diff === null) {
$key = $k;
$diff = $d;
continue;
}
if ($d === $diff && $v > $TEMPS[$key]) {
$key = $k;
$diff = $d;
continue;
}
if ($d < $diff) {
$key = $k;
$diff = $d;
}
}
// Write an action using echo(). DON'T FORGET THE TRAILING \n
// To debug (equivalent to var_dump): error_log(var_export($var, true));
echo $TEMPS[$key], "\n";
?>

View File

@ -0,0 +1,61 @@
<?php
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
fscanf(STDIN, "%d",
$N // the number of points used to draw the surface of Mars.
);
for ($i = 0; $i < $N; $i++)
{
fscanf(STDIN, "%d %d",
$LAND_X, // X coordinate of a surface point. (0 to 6999)
$LAND_Y // Y coordinate of a surface point. By linking all the points together in a sequential fashion, you form the surface of Mars.
);
}
function R($R, $SPEED) {
echo $R, ' ', $SPEED, "\n";
}
// game loop
while (TRUE)
{
fscanf(STDIN, "%d %d %d %d %d %d %d",
$X,
$Y,
$HS, // the horizontal speed (in m/s), can be negative.
$VS, // the vertical speed (in m/s), can be negative.
$F, // the quantity of remaining fuel in liters.
$R, // the rotation angle in degrees (-90 to 90).
$P // the thrust power (0 to 4).
);
if (abs($HS) < 70 && $Y < 2200) {
if ($Y < 200) {
r(0, 4);
} else {
if ($LAND_X < $X) {
r(20, 4);
} else {
r(-20, 4);
}
}
}
else {
if (abs($R) < 40) {
$SPEED = 0;
} else {
$SPEED = 4;
}
$q = $HS > 0 ? -1 : 1;
if ($LAND_X < $X) {
r(50 * $q, $SPEED);
} else {
r(-50 * $q, $SPEED);
}
}
}
?>

View File

@ -0,0 +1,111 @@
<?php
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
fscanf(STDIN, "%d",
$n // the number of adjacency relations
);
class Node
{
protected $children = [];
protected $parent = null;
public function addChild(Node $node)
{
$node->setParent($this);
$this->children[] = $node;
}
public function getChildren()
{
return $this->children;
}
public function setParent(Node $node)
{
$this->parent = $node;
}
public function getParent()
{
return $this->parent;
}
}
class Nodes
{
protected static $nodes = [];
public static function addNode($key, Node $node)
{
self::$nodes[$key] = $node;
}
public static function get($key)
{
return isset(self::$nodes[$key]) ? self::$nodes[$key] : null;
}
public static function getNodes()
{
return self::$nodes;
}
}
$rootNode = null;
for ($i = 0; $i < $n; $i++)
{
fscanf(STDIN, "%d %d",
$xi, // the ID of a person which is adjacent to yi
$yi // the ID of a person which is adjacent to xi
);
$parent = Nodes::get($xi);
$child = Nodes::get($yi);
if (null === $parent) {
$parent = new Node();
Nodes::addNode($xi, $parent);
if ($rootNode === null) {
$rootNode = $parent;
}
}
if (null === $child) {
$child = new Node();
Nodes::addNode($yi, $child);
}
$parent->addChild($child);
}
$depth = 0;
foreach (Nodes::getNodes() as $k => $node) {
if ($node->getChildren()) {
continue;
}
$d = 0;
$p = $node->getParent();
while ($p) {
$d++;
$p = $p->getParent();
}
if ($d > $depth) {
$depth = $d;
}
}
// Write an action using echo(). DON'T FORGET THE TRAILING \n
// To debug (equivalent to var_dump): error_log(var_export($var, true));
echo("$depth\n"); // The minimal amount of steps required to completely propagate the advertisement
?>