2011-02-23 20:37:49
<?php
 
error_reporting(E_ALL);
 
class bullshit {
 
    public $path;
    public $total_engineers;
    public $total_drinks;
    public $engineer = array();
    public $drink = array();
    public $scoreboard;
    public $enable_output = true;
    private $io;
 
    public function __construct($p = NULL) {
        // setup the default variables
        $this->total_engineers = 0;
        $this->total_drinks = 0;
        $this->path = (isset($p)) ? $p : die("Please check the file path is valid or not.");
    }
 
    /*
     * read file
     */
 
    public function __readfile() {
        $this->io = fopen($this->path, "r") or die("the path that you've assigned is invalid.({$this->path})");
        flock($this->io, 2);
    }
 
    /*
     * set global total drinks number
     */
 
    private function __setTotalDrinks($value) {
        $this->total_drinks = $value;
    }
 
    /*
     * set global total engineers number
     */
 
    private function __setTotalEngineers($value) {
        $this->total_engineers = $value;
    }
 
    /*
     * convert from string to array using seperator
     */
 
    public function __getValueBySeperator($string, $seperator) {
        $string_array = explode(",", $string);
        return $string_array;
    }
 
    /*
     * We read each line to:
     *      1. define first line to set the total engineer and total drinks ( seperator by a space ) ( total engineer is always even number )
     */
 
    public function __readline($maxline) {
        $current_row = 0;
        while ($line = fgets($this->io, 4096)) { // read to the end of the file
            try {
                ++$current_row;
                if (strlen($line) > 2) {
                    if ($current_row == 1) { // handling the first row
                        $element = explode(" ", $line);
                        $this->__setTotalEngineers($element[0]);
                        $this->__setTotalDrinks($element[1]);
 
                        if ($this->total_engineers % 2 != 0)
                            die("Total engineers must be an even number");
 
                        for ($i = 0; $i < $this->total_engineers; $i++) {
                            for ($x = 0; $x < $this->total_drinks; $x++) {
                                $this->engineer[$i][$x] = -1;
                            }
                        }
 
                        for ($i = 0; $i < $this->total_drinks; $i++) {
                            $this->drink[$i] = "";
                        }
                    } else if ($current_row > 1 && $current_row <= ($this->total_drinks + 1)) { // rows between total drink range
 
                        $element = explode("\t", $line); // get element by seperator tab
												$element[1] = str_replace("\n", "", $element[1]);
                        $this->drink[$element[0]] = trim($element[1]); // push the value into the drink array
                    } else if ($current_row > ($this->total_drinks + 1) && $current_row <= ($this->total_engineers + $this->total_drinks + 1)) { // row between engineer ranges
                        $element = explode("\t", $line); // get element by seperator tab
                        $element[1] = str_replace("\n", "", $element[1]);
 
                        $drinks = explode(",", $element[1]);
                        foreach ($drinks as $key => $value) {
                            $this->engineer[$element[0]][$key] = trim($value);
                        }
                    }
 
 
                    $this->__output($element[0] . " " . $element[1] . "<br/>");
                }
            } catch (Exception $e) {
                echo "The input file is not well-formed: " . $e->getMessage();
            }
        }
    }
 
    /*
     * We need to assign drink number into 2 dimension array which first dimension is id of engineer
     * second dimension indicates id the engineer's drink of choice
     * eg.
     *      Engineer[0][0] = 1
     *      Engineer[0][1] = 0
     *      this engineer has chosen his first drink is 1. second drink of his is 0. And so on.
     */
 
    public function __displayDrinkToEngineer() {
        for ($i = 0; $i < $this->total_engineers; $i++) {
            echo "e[" . $i . "]=";
            for ($x = 0; $x < $this->total_drinks; $x++) {
                if ($this->engineer[$i][$x] != NULL)
                    echo $this->engineer[$i][$x] . ",";
            }
            echo "<br/>";
        }
    }
 
    public function __setupScoreBoard() {
 
        //Array for chosen engineers
        $total_chosen = 0;
        $flag1 = 0;
        for ($i = 0; $i < $this->total_engineers / 2; $i++) {
            $chosen[$i] = 0;
            for ($o = 0; $o < 2; $o++)
                $this->scoreboard[$i][$o] = "";
        }
        //Loop for the upper group of engineers
        for ($i = 0; $i < $this->total_engineers / 2; $i++) {
            //Loop for taking preference score between 2 Engineers
            for ($j = $this->total_engineers - 1; $j > ($this->total_engineers / 2 - 1); $j--) {
                //Starting score
                $score = 0;
                //Loop for every drink in the upper group of engineers
                for ($k = 0; $k < $this->total_drinks; $k++) {
                    //Checking for chosen engineers
                    if ($total_chosen > 0) {
                        for ($x = 0; $x < $total_chosen; $x++) {
                            if ($j == $chosen[$x]) {
                                $flag1 = 1;
                            }
                        }
                        if ($flag1 == 1) {
                            $flag1 = 0;
                            break;
                        }
                    }
 
                    if ($this->engineer[$i][$k] == -1)
                        break;
 
                    //Loop for taking preference score between 2 drinks
                    for ($l = 0; $l < $this->total_drinks; $l++) {
                        if ($this->engineer[$i][$k] == $this->engineer[$j][$l]) {
                            //check position
                            if ($k == $l) {
                                $score += ( $this->total_drinks - $k) * ($this->total_drinks - $k);
 
                                $this->__output("+" . ($this->total_drinks - $k) * ($this->total_drinks - $k) . "<br/>"); //Debug
 
                                break;
                            } else if ($k < $l) {
                                $score += ( $this->total_drinks - $k);
 
                                $this->__output("+" . ($this->total_drinks - $k) . "<br/>"); //Debug
 
                                break;
                            } else {
                                break;
                            }
                        } 
												else {
                            if ($l == ($this->total_drinks) - 1) { 
                                $score += ( $this->total_drinks - $k);
 
                                $this->__output("+" . ($this->total_drinks - $k) . "<br/>"); //Debug do o day no' + sai o.o ko sai dau .... 
 
                                break;
                            }
                        }
                    }
                }
 
                $this->__output("E[" . $i . "] and E[" . $j . "] Preference Score:" . $score . "<br/>"); //Debug purpose
                //Score update
                if ($score >= $this->scoreboard[$i][0]) {
                    $this->scoreboard[$i][0] = $score;
                    $this->scoreboard[$i][1] = $j;
                }
            }
            //Add the chosen
            $chosen[$i] = $this->scoreboard[$i][1];
 
            $this->__output("E[" . $i . "] choose E[" . $this->scoreboard[$i][1] . "] with Highest Preference Score:" . $this->scoreboard[$i][0] . "<br/>"); // o day may` lam` mat' 1 do`ng debug
 
            $total_chosen++;
        }
    }
 
    public function __getEngineerDrinkNames($EngineerID) {
        $text = "";
        foreach ($this->engineer[$EngineerID] as $key => $value) {
            if ($value != -1)
                $text .= $this->drink[$value] . "[{$value}],";
        }
        $text = substr($text, 0, -1);
        return $text;
    }
 
    public function __output($string) {
        if ($this->enable_output == true) {
            echo $string;
        }
        else
            return "";
    }
 
}
 
$bullshit = new bullshit("inputfile.txt");
$bullshit->enable_output = true;
 
// initialization
$bullshit->__readfile();
$bullshit->__readline(1000);
 
$bullshit->__output("<html><head><title>asdasd</title></head><body>");
$bullshit->__output("<h1>DEBUG</h1>");
 
echo("Total Engineers: {$bullshit->total_engineers}<br/>");
echo("Total Drinks: {$bullshit->total_drinks}<br/><br/>");
 
$bullshit->__displayDrinkToEngineer();
 
echo("<h1>SCORED BOARD</h1>");
echo $bullshit->__setupScoreBoard();
 
$bullshit->__output("<br/>");
 
for ($i = 0; $i < ($bullshit->total_engineers / 2); $i++) {
    echo($i . " " . $bullshit->scoreboard[$i][1] . "<br/>");
}
 
$bullshit->__output("<br/>");
 
for ($i = 0; $i < ($bullshit->total_engineers / 2); $i++) {
    $bullshit->__output("E[{$i}] " . $bullshit->__getEngineerDrinkNames($i) . " _______ E[{$bullshit->scoreboard[$i][1]}] " . $bullshit->__getEngineerDrinkNames($bullshit->scoreboard[$i][1]) . "<br/>");
    $bullshit->__output("<br/>");
}
 
$bullshit->__output("</body>");
$bullshit->__output("</html>");
?>
Invalid Email or Password