Skip to content

Commit 944cc65

Browse files
committed
Day 8, part2
1 parent af970ab commit 944cc65

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

src/Days/Day8.php

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,73 @@ public function solve_part_1(): string
8484

8585
public function solve_part_2(): string
8686
{
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;
88154
}
89155

90156
protected function getTreeGrid()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy