Skip to content

Commit a685df3

Browse files
committed
Day 8
1 parent bf35b0f commit a685df3

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed

src/test/java/com/macasaet/Day08.java

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package com.macasaet;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.stream.Collectors;
6+
import java.util.stream.StreamSupport;
7+
8+
/**
9+
* --- Day 8: Treetop Tree House ---
10+
* <a href="https://adventofcode.com/2022/day/8">https://adventofcode.com/2022/day/8</a>
11+
*/
12+
public class Day08 {
13+
14+
record Forest(int[][] grid) {
15+
public int countVisible() {
16+
int result = 0;
17+
for(int i = grid().length; --i >= 0; ) {
18+
final var row = grid()[i];
19+
for(int j = row.length; --j >= 0; ) {
20+
if(isVisible(i, j)) {
21+
result++;
22+
}
23+
}
24+
}
25+
return result;
26+
}
27+
28+
public int scenicScore(final int x, final int y) {
29+
final int treeHeight = grid()[x][y];
30+
int northScore = 0;
31+
for(int i = x; --i >= 0; ) {
32+
final var height = grid()[i][y];
33+
northScore += 1;
34+
if(height >= treeHeight) {
35+
break;
36+
}
37+
}
38+
int southScore = 0;
39+
for(int i = x + 1; i < grid().length; i++) {
40+
final var height = grid()[i][y];
41+
southScore += 1;
42+
if(height >= treeHeight) {
43+
break;
44+
}
45+
}
46+
int westScore = 0;
47+
for(int j = y; --j >= 0; ) {
48+
final var height = grid()[x][j];
49+
westScore += 1;
50+
if(height >= treeHeight) {
51+
break;
52+
}
53+
}
54+
int eastScore = 0;
55+
for(int j = y + 1; j < grid()[x].length; j++) {
56+
final var height = grid()[x][j];
57+
eastScore += 1;
58+
if(height >= treeHeight) {
59+
break;
60+
}
61+
}
62+
return northScore * eastScore * southScore * westScore;
63+
}
64+
65+
boolean isVisible(final int x, final int y) {
66+
if(x == 0 || x == grid().length || y == 0 || y == grid()[x].length) {
67+
// trees on the edge
68+
return true;
69+
}
70+
final int treeHeight = grid()[x][y];
71+
if (!isObstructedFromTheNorth(x, y, treeHeight)) {
72+
return true;
73+
}
74+
if (!isObstructedFromTheSouth(x, y, treeHeight)) {
75+
return true;
76+
}
77+
if (!isObstructedFromTheWest(x, y, treeHeight)) {
78+
return true;
79+
}
80+
if (!isObstructedFromTheEast(x, y, treeHeight)) {
81+
return true;
82+
}
83+
return false;
84+
}
85+
86+
private boolean isObstructedFromTheEast(int x, int y, int treeHeight) {
87+
for(int j = grid()[x].length; --j > y; ) {
88+
if(grid()[x][j] >= treeHeight) {
89+
return true;
90+
}
91+
}
92+
return false;
93+
}
94+
95+
private boolean isObstructedFromTheWest(int x, int y, int treeHeight) {
96+
for(int j = y; --j >= 0; ) {
97+
if(grid()[x][j] >= treeHeight) {
98+
return true;
99+
}
100+
}
101+
return false;
102+
}
103+
104+
private boolean isObstructedFromTheSouth(int x, int y, int treeHeight) {
105+
for(int i = grid().length; --i > x; ) {
106+
if(grid()[i][y] >= treeHeight) {
107+
return true;
108+
}
109+
}
110+
return false;
111+
}
112+
113+
private boolean isObstructedFromTheNorth(int x, int y, int treeHeight) {
114+
for(int i = x; --i >= 0; ) {
115+
if(grid()[i][y] >= treeHeight) {
116+
return true;
117+
}
118+
}
119+
return false;
120+
}
121+
}
122+
123+
protected Forest getInput() {
124+
final var list = StreamSupport
125+
.stream(new LineSpliterator("day-08.txt"),
126+
false)
127+
.map(line -> {
128+
final var chars = line.toCharArray();
129+
final var row = new int[chars.length];
130+
for(int i = chars.length; --i >= 0; row[i] = chars[i] - '0');
131+
return row;
132+
})
133+
.collect(Collectors.toList());
134+
final var grid = new int[list.size()][];
135+
for(int i = list.size(); --i >= 0; grid[i] = list.get(i));
136+
return new Forest(grid);
137+
}
138+
139+
@Test
140+
public final void part1() {
141+
final var forest = getInput();
142+
final var result = forest.countVisible();
143+
System.out.println("Part 1: " + result);
144+
}
145+
146+
@Test
147+
public final void part2() {
148+
final var forest = getInput();
149+
int result = Integer.MIN_VALUE;
150+
for(int i = forest.grid().length; --i >= 0; ) {
151+
for( int j = forest.grid.length; --j >= 0; ) {
152+
final var score = forest.scenicScore(i, j);
153+
if(score > result) {
154+
result = score;
155+
}
156+
}
157+
}
158+
System.out.println("Part 2: " + result);
159+
}
160+
161+
}

src/test/resources/sample/day-08.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
30373
2+
25512
3+
65332
4+
33549
5+
35390

0 commit comments

Comments
 (0)