-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_eight.php
79 lines (75 loc) · 2.64 KB
/
day_eight.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
<?php
// input_day_eight
$filePointer = fopen("input_day_eight.txt", "r");
$forest = [];
$index = 0;
while (!feof($filePointer) && $line = trim(fgets($filePointer)))
{
$forest[$index] = str_split($line);
$index++;
}
$visibleTreesCount = 4 * count($forest) - 4;
$highestTreeScore = 0;
for ($i = 1; $i < count($forest)-1; $i++) {
for ($j = 1; $j < count($forest)-1; $j++) {
echo "$i $j " . $forest[$i][$j] . "\n";
$topCount = 0;
$bottomCount = 0;
$leftCount = 0;
$rightCount = 0;
$hiddenTop = false;
$hiddenBottom = false;
$hiddenLeft = false;
$hiddenRight = false;
foreach ($forest as $key => $treeLine) {
if ($key < $i) {
$topCount++;
}
if ($key > $i && !$hiddenBottom) {
$bottomCount++;
}
if ($key < $i && $treeLine[$j] >= $forest[$i][$j]) {
// versteckt von oben
$topCount = 1;
$hiddenTop = true;
}
if ($key > $i && $treeLine[$j] >= $forest[$i][$j]) {
// versteckt von unten
$hiddenBottom = true;
}
if ($key == $i) {
$leftTrees = array_slice($treeLine, 0, $j);
foreach (array_reverse($leftTrees) as $leftTree) {
$leftCount++;
if ($leftTree >= $forest[$i][$j]) {
break;
}
}
if (max($leftTrees) >= $forest[$i][$j]) {
// versteckt von links
$hiddenLeft = true;
}
$rightTrees = array_slice($treeLine, $j+1);
foreach ($rightTrees as $rightTree) {
$rightCount++;
if ($rightTree >= $forest[$i][$j]) {
break;
}
}
if (max($rightTrees) >= $forest[$i][$j]) {
// versteckt von rechts
$hiddenRight = true;
}
}
}
echo "top $topCount bottom $bottomCount right $rightCount left $leftCount\n";
echo "result: " . $topCount*$bottomCount*$rightCount*$leftCount . "\n";
$highestTreeScore = max($topCount*$bottomCount*$rightCount*$leftCount, $highestTreeScore);
if ($hiddenTop == false || $hiddenBottom == false || $hiddenRight == false || $hiddenLeft == false) {
echo "visible tree \n";
$visibleTreesCount++;
}
}
}
echo "$visibleTreesCount trees are visible\n";
echo "$highestTreeScore is the best score\n";