-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday0801.php
89 lines (82 loc) · 2.04 KB
/
day0801.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
$input = fopen("forrest.txt", "r");
$forrest = array();
$i=0;
$numtrees = 0;
while (($line = fgets($input)) !== false) {
$forrest[$i] = str_split(substr($line, 0, strlen($line)-1));
$i++;
}
$numtrees = $i*count($forrest[0])-(($i-2)*(count($forrest[0])-2));
echo $numtrees." num of trees on the edge<br>";
$rows = count($forrest);
$cols = count($forrest[0]);
for ($row = 1; $row < $rows-1; $row++) {
$cols = count($forrest[$row]);
for($col = 1; $col < $cols-1; $col++ ) {
if (checkPrevRow($row,$col)==TRUE){
$numtrees+=1;
}elseif(checkNextRow($row,$col)==TRUE){
$numtrees+=1;
}elseif(checkPrevCol($row,$col)==TRUE){
$numtrees+=1;
}elseif(checkNextCol($row,$col)==TRUE){
$numtrees+=1;
}
}
}
echo "Total visible trees: ".$numtrees."<br>";
function checkPrevRow($row, $col){
global $forrest;
$visible = FALSE;
for($i=0; $i<$col; $i++){
if ($forrest[$row][$i]<$forrest[$row][$col]){
$visible = TRUE;
} else {
$visible = FALSE;
break;
}
}
return $visible;
}
function checkPrevCol($row, $col){
global $forrest;
$visible = FALSE;
for($i=0; $i<$row; $i++){
if ($forrest[$i][$col]<$forrest[$row][$col]){
$visible = TRUE;
} else {
$visible = FALSE;
break;
}
}
return $visible;
}
function checkNextRow($row, $col){
global $forrest;
global $cols;
$visible = FALSE;
for($i=$cols-1; $i>$col; $i--){
if ($forrest[$row][$i]<$forrest[$row][$col]){
$visible = TRUE;
} else {
$visible = FALSE;
break;
}
}
return $visible;
}
function checkNextCol($row, $col){
global $forrest;
global $rows;
$visible = FALSE;
for($i=$rows-1; $i>$row; $i--){
if ($forrest[$i][$col]<$forrest[$row][$col]){
$visible = TRUE;
} else {
$visible = FALSE;
break;
}
}
return $visible;
}