Exemple #1
0
function updateGrid($grid, $num, $stuck = [], $show = false)
{
    if ($num < 1) {
        return $grid;
    }
    $newGrid = $grid;
    $xnum = count($grid);
    $ynum = count(reset($grid));
    foreach ($grid as $i => $row) {
        foreach ($row as $j => $on) {
            if (in_array([$i, $j], $stuck)) {
                continue;
            }
            $minX = max(1, $i - 1);
            $maxX = min($xnum, $i + 1);
            $minY = max(1, $j - 1);
            $maxY = min($ynum, $j + 1);
            $neighbours = 0;
            for ($m = $minX; $m <= $maxX; $m++) {
                for ($n = $minY; $n <= $maxY; $n++) {
                    if ($m == $i && $n == $j) {
                        continue;
                    }
                    $neighbours += $grid[$m][$n] ? 1 : 0;
                }
            }
            if ($on) {
                $newGrid[$i][$j] = $neighbours == 2 || $neighbours == 3;
            } else {
                $newGrid[$i][$j] = $neighbours == 3;
            }
        }
    }
    if ($show) {
        printGrid($newGrid);
    }
    return updateGrid($newGrid, $num - 1, $stuck, $show);
}
Exemple #2
0
    foreach ($ngrid as $y => $row) {
        foreach ($row as $x => $n) {
            if ($grid[$y][$x]) {
                //A light which is on stays on when 2 or 3 neighbors are on, and turns off otherwise.
                $newGrid[$y][$x] = $n == 2 || $n == 3;
            } else {
                //A light which is off turns on if exactly 3 neighbors are on, and stays off otherwise.
                $newGrid[$y][$x] = $n == 3;
            }
        }
    }
    if (PART_2) {
        return stuckLights($newGrid);
    }
    return $newGrid;
}
//loop
for ($i = 0; $i < $steps; $i++) {
    $grid = iterate($grid);
}
//result
$count = 0;
foreach ($grid as $y => $row) {
    foreach ($row as $x => $on) {
        if ($on) {
            $count++;
        }
    }
}
printGrid($grid);
print "Lights lit: {$count}\n";
$steps = 100;
$currentGrid = [];
foreach ($lines as $line) {
    $line = str_replace('#', 1, $line);
    $line = str_replace('.', 0, $line);
    $chars = str_split($line);
    $currentGrid[] = $chars;
}
// Step 2. Force all 4 corners ON
$size = count($currentGrid[0]);
$currentGrid[0][0] = 1;
$currentGrid[0][$size - 1] = 1;
$currentGrid[$size - 1][0] = 1;
$currentGrid[$size - 1][$size - 1] = 1;
echo "initial state" . PHP_EOL;
printGrid($currentGrid);
while ($steps > 0) {
    $steps--;
    $nextGrid = $currentGrid;
    for ($i = 0; $i < count($currentGrid); $i++) {
        for ($j = 0; $j < count($currentGrid[$i]); $j++) {
            $nextGrid[$i][$j] = determineNextState($i, $j);
        }
    }
    $currentGrid = $nextGrid;
}
$totalOn = 0;
for ($i = 0; $i < count($currentGrid); $i++) {
    $totalOn += array_sum($currentGrid[$i]);
}
echo "Total lights on: " . $totalOn . PHP_EOL;
Exemple #4
0
		echo "<div class=level3>" . $group["level3"] . "</div>";
	//-----------------------------------------------------------
	$GroupID = $group["levelcode3"] != "" ? $group["levelcode3"] : $group["levelcode2"];
	
	$dt = PdoDataAccess::runquery("select e.*,p.ElementValue
		from PLN_Elements e
		left join PLN_PlanItems p on(PlanID=? AND p.ElementID=e.ElementID)
		
		where GroupID=? AND ParentID=0 
		group by ElementID
		order by ElementID", array($PlanID, $GroupID));
	
	for($i=0; $i < count($dt); $i++)
	{
		if($dt[$i]["ElementType"] == "grid")
			printGrid($PlanID, $dt[$i]["ElementID"]);
		
		if($dt[$i]["ElementType"] == "panel")
			printForm($dt[$i]["ElementID"], $dt[$i]["ElementValue"]);		
	}
}

function printGrid($PlanID, $ParentID){

	$columns = PdoDataAccess::runquery("select * from PLN_Elements e 
		where ParentID=? order by ElementID", array($ParentID));
	
	echo "<center><table class=form border=1><tr>";
	foreach($columns as $col)
		echo "<td class=titles>" . $col["ElementTitle"] . "</td>";
	echo "</tr>";