-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2d-array.php
39 lines (29 loc) · 881 Bytes
/
2d-array.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
// https://www.hackerrank.com/challenges/2d-array/problem
function hourglassSum($arr) {
$max = -63;
for ($i = 0; $i < 4; $i++) {
for ($j = 0; $j < 4; $j++) {
$sum = $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];
$max = max($sum, $max);
}
}
return $max;
}
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$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));
}
$result = hourglassSum($arr);
fwrite($fptr, $result . "\n");
fclose($stdin);
fclose($fptr);