Skip to content

Instantly share code, notes, and snippets.

@kpx-dev
Created April 14, 2012 17:31
Show Gist options
  • Save kpx-dev/2386118 to your computer and use it in GitHub Desktop.
Save kpx-dev/2386118 to your computer and use it in GitHub Desktop.
Solution to: Problem C. Recycled Numbers
<?php
/**
* Google Code Jam 2012
*
* @author Kien Pham - kien@sendgrid.com
*/
$C = new CodeJam();
$C->run();
class CodeJam
{
private $_filename = 'in.in';
private $_data;
private $_cases;
private $_file_out_handle;
public function algorithm3($data, $case)
{
$data = trim($data[0]);
$data = explode(' ', $data);
$A = $data[0];
$B = $data[1];
$current_n = null;
$current_m = null;
// 1 digit, no solution:
if (strlen($A) == 1 and strlen($B) == 1)
{
return 0;
}
$result = array();
$ranges = range($A, $B);
$count = 0;
for ($i = 0; $i < count($ranges); $i++)
{
// $ranges[$i] = '12345'; // 51234, 45123, 34512, 23451 n - 1 case
$current_n = $ranges[$i];
$current_m = $ranges[$i];
$length = strlen($ranges[$i]);
for ($j = 0; $j < $length; $j++)
{
// rotate string to get m value:
$current_m = (int)substr($current_m, -1) . substr($current_m, 0, $length - 1);
// A ≤ n < m ≤ B
if ($current_m <= $B and $current_m > $current_n and $current_m >= $A)
{
$result[$current_n . $current_m] = true;
//echo "($current_n, $current_m) . count: " . count($result) . " \n";
}
}
}
//print_r($data);exit;
return count($result);
}
public function algorithm2($data, $case)
{
$data = trim($data[0]);
$data = explode(' ', $data);
$people = (int)$data[0];
$surprise = (int)$data[1];
$min_score = (int)$data[2];
unset($data[0], $data[1], $data[2]);
//sort($data);
$scores = $data;
$cases = 0;
foreach ($scores as $score)
{
echo 'find 3 judges score for score: ' . $score . "\n";
$base = (int)($score / 3);
$result = array();
switch ($score % 3)
{
case 0:
{
$result = array(
array($base, $base, $base),
array($base - 1, $base, $base + 1)
);
// regular case:
if ($base >= $min_score)
{
$result['case'] = true;
$cases++;
}
else
{
// check for surprise case:
if ($surprise > 0 and $base > 0 and $base + 1 >= $min_score)
{
$cases++;
$surprise--;
$result['case'] = true;
}
}
$result['surprise'] = $surprise;
break;
}
case 1:
{
$result = array(
array($base, $base, $base+1),
array($base-1, $base+1, $base+1),
);
// regular case:
if ($base >= $min_score or $base + 1 >= $min_score)
{
$cases++;
$result['case'] = true;
}
else
{
// surprise case:
if ($surprise > 0 and $base + 1 >= $min_score)
{
$result['case'] = true;
$cases++;
$surprise--;
}
}
$result['surprise'] = $surprise;
break;
}
case 2:
{
$result = array(
array($base, $base, $base + 2),
array($base, $base + 1, $base + 1)
);
// regular case:
if ($base + 1 >= $min_score or $base >= $min_score)
{
$result['case'] = true;
$cases++;
}
else
{
if ($surprise > 0 and $base + 2 >= $min_score)
{
$result['case'] = true;
$cases++;
$surprise--;
}
}
$result['surprise'] = $surprise;
break;
}
}
print_r($result);
}
echo 'surprises: ' . $surprise . "\n";
//echo 'cases: ' . $cases; exit;
return $cases;
}
public function algorithm1($data, $case)
{
// 'a' -> 'y', 'o' -> 'e', and 'z' -> 'q'.
// q, z
$string = 'ejp mysljylc kd kxveddknmc re jsicpdrysi rbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcd de kr kd eoya kw aej tysr re ujdr lkgc jv y e q z';
$string = str_split($string);
$answer = 'our language is impossible to understand there are twenty six factorial possibilities so it is okay if you want to just give up a o z q';
$answer = str_split($answer);
$maps = array();
foreach ($string as $key => $val)
{
$maps[$val] = $answer[$key];
}
ksort($maps);
//print_r($maps);exit;
$string = trim($data[0]);
echo 'processing case # ' . $case . "\n";
echo 'items is: ' . $string . "\n";
echo 'result is: ';
$result = null;
$strings = str_split($string);
foreach ($strings as $string)
{
$result .= $maps[$string]; // isset($maps[$string]) ? $maps[$string]:' ';
}
//echo $result;exit;
return $result;
}
public function __construct()
{
$this->_filename = $_SERVER['argv'][1];
}
public function run()
{
// remove output file:
$this->removeFile($this->_filename . '.out');
$data = $this->readFile();
// call algorithm here:
//$data = $this->algorithm1($data);
// write to file:
//$this->writeFile($data);
// close output file:
$this->closeFile();
}
public function readFile()
{
$file_handle = fopen($this->_filename, 'r');
$case = 1;
$data = array();
do
{
if (!$this->_cases)
{
$this->_cases = fgets($file_handle);
}
else
{
$current_case = array(fgets($file_handle));
$indexes = $this->algorithm3($current_case, $case);
$this->writeFile($indexes, $case);
$case++;
}
}
while (!feof($file_handle) and $case <= $this->_cases);
$content = file_get_contents($this->_filename);
return $content;
}
public function writeFile($data, $case)
{
$out = "Case #$case: $data\n";
echo $out;
$this->_file_out_handle = fopen($this->_filename . '.out', 'a');
fwrite($this->_file_out_handle, $out);
}
public function closeFile()
{
fclose($this->_file_out_handle);
}
public function removeFile($file)
{
if (file_exists($file))
{
unlink($file);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment