Day 11: 2D Arrays

less than 1 minute read

https://www.hackerrank.com/challenges/30-binary-numbers/problem

<?php
$stdin = fopen("php://stdin", "r");

$arr = array();

for ($i = 0; $i < 6; $i++) {
    fscanf($stdin, "%[^\n]", $arr_temp);
    $arr[] = array_map('intval', preg_split('/ /', $arr_temp, -1, PREG_SPLIT_NO_EMPTY));
}
$max = '';
for($i=0;$i<count($arr)-2;$i++){
    for($j=0;$j<count($arr[$i])-2;$j++){
        $hourglasses = $arr[$i][$j]+$arr[$i][$j+1]+$arr[$i][$j+2]
                            +$arr[$i+1][$j+1]
                +$arr[$i+2][$j]+$arr[$i+2][$j+1]+$arr[$i+2][$j+2];
        if($max === ''){
            $max = $hourglasses;
        } else {
            $max = $max>$hourglasses?$max:$hourglasses;
        }
    }
}

echo $max;

fclose($stdin);