@@ -84,7 +84,73 @@ public function solve_part_1(): string
84
84
85
85
public function solve_part_2 (): string
86
86
{
87
- return "TODO " ;
87
+ $ trees = $ this ->getTreeGrid ();
88
+ $ highestScore = 0 ;
89
+ $ height = count ($ trees );
90
+ $ width = count ($ trees [0 ]);
91
+
92
+ for ($ x = 1 ; $ x < $ width - 1 ; $ x += 1 ) {
93
+ for ($ y = 1 ; $ y < $ height - 1 ; $ y += 1 ) {
94
+ $ highestScore = max ($ highestScore , $ this ->calcScore ($ trees , $ x , $ y , $ width , $ height ));
95
+ }
96
+ }
97
+
98
+ return $ highestScore ;
99
+ }
100
+
101
+ protected function calcScore ($ trees , $ startx , $ starty , $ width , $ height )
102
+ {
103
+ $ treeHeight = $ trees [$ starty ][$ startx ];
104
+
105
+ // right
106
+ $ scoreRight = 0 ;
107
+ for ($ x = $ startx + 1 ; $ x < $ width ; $ x += 1 ) {
108
+ if ($ trees [$ starty ][$ x ] < $ treeHeight ) {
109
+ $ scoreRight += 1 ;
110
+ }
111
+ if ($ trees [$ starty ][$ x ] >= $ treeHeight ) {
112
+ $ scoreRight += 1 ;
113
+ break ;
114
+ }
115
+ }
116
+
117
+ // left
118
+ $ scoreLeft = 0 ;
119
+ for ($ x = $ startx - 1 ; $ x >= 0 ; $ x -= 1 ) {
120
+ if ($ trees [$ starty ][$ x ] < $ treeHeight ) {
121
+ $ scoreLeft += 1 ;
122
+ }
123
+ if ($ trees [$ starty ][$ x ] >= $ treeHeight ) {
124
+ $ scoreLeft += 1 ;
125
+ break ;
126
+ }
127
+ }
128
+
129
+ // down
130
+ $ scoreDown = 0 ;
131
+ for ($ y = $ starty + 1 ; $ y < $ height ; $ y += 1 ) {
132
+ if ($ trees [$ y ][$ startx ] < $ treeHeight ) {
133
+ $ scoreDown += 1 ;
134
+ }
135
+ if ($ trees [$ y ][$ startx ] >= $ treeHeight ) {
136
+ $ scoreDown += 1 ;
137
+ break ;
138
+ }
139
+ }
140
+
141
+ // up
142
+ $ scoreUp = 0 ;
143
+ for ($ y = $ starty - 1 ; $ y >= 0 ; $ y -= 1 ) {
144
+ if ($ trees [$ y ][$ startx ] < $ treeHeight ) {
145
+ $ scoreUp += 1 ;
146
+ }
147
+ if ($ trees [$ y ][$ startx ] >= $ treeHeight ) {
148
+ $ scoreUp += 1 ;
149
+ break ;
150
+ }
151
+ }
152
+
153
+ return $ scoreRight * $ scoreLeft * $ scoreDown * $ scoreUp ;
88
154
}
89
155
90
156
protected function getTreeGrid ()
0 commit comments