-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay8.java
More file actions
169 lines (157 loc) · 3.75 KB
/
Day8.java
File metadata and controls
169 lines (157 loc) · 3.75 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
public class Day8 {
public static String myRead(Scanner scanner) {
String line;
try {
line = scanner.nextLine().trim();
} catch (Exception e) {
line = null;
}
return line;
}
public static Scanner myScanner(File text) {
Scanner scanner;
try {
scanner = new Scanner(text);
} catch (Exception e) {
System.out.println("No Scanner");
return null;
}
return scanner;
}
public static void main(String[] args) {
File text = new File("/Users/axelteo/Desktop/AdventofCode/Day8input.txt");
Scanner scanner = myScanner(text);
if (scanner == null) {
return;
}
int[][] input = new int[150][150];
for (int i = 0; i < 150; ++i) {
for (int j = 0; j < 150; ++j) {
input [i][j] = -1;
}
}
// Reading tree heights into a 2d array
String line;
int lineCount = 0;
while ((line = myRead(scanner)) != null) {
int lineLen = line.length();
for (int col = 0; col < lineLen; ++col) {
input[lineCount][col] = Character.getNumericValue(line.charAt(col));
}
++lineCount;
}
// Part 1 solution
int visTrees = numVisible(input);
System.out.println("num of visible trees: " + visTrees);
// Part 2 Solution
int maxScore = 0;
for (int i = 0; i < 150; ++i) {
for (int j = 0; j < 150; ++j) {
int tmp = getScore(input, i, j);
if (tmp > maxScore) {
maxScore = tmp;
}
}
}
System.out.println("max score is: " + maxScore);
}
public static int getScore(int[][] input, int row, int col) {
int top, down, left, right;
top = down = left = right = 0;
int height = input[row][col];
for (int i = row - 1; i >= 0; --i) {
if (input[i][col] == -1) {
break;
}
++left;
if (height <= input[i][col]) {
break;
}
}
for (int i = row + 1; i < 150 ; ++i) {
if (input[i][col] == -1) {
break;
}
++right;
if (height <= input[i][col]) {
break;
}
}
for (int i = col - 1; i >= 0; --i) {
if (input[row][i] == -1) {
break;
}
++top;
if (height <= input[row][i]) {
break;
}
}
for (int i = col + 1; i < 150; ++i) {
if (input[row][i] == -1) {
break;
}
++down;
if (height <= input[row][i]) {
break;
}
}
return top * down * left * right;
}
public static int numVisible(int[][] input) {
int[][] output = new int[150][150];
for (int i = 0; i < 150; ++i) {
for (int j = 0; j < 150; ++j) {
output[i][j] = 0;
}
}
for (int row = 0; row < 150; ++row) {
int highest = -1;
for (int col = 0; col < 150; ++col) {
if (input[row][col] > highest) {
output[row][col] = 1;
highest = input[row][col];
}
}
}
for (int row = 0; row < 150; ++row) {
int highest = -1;
for (int col = 149; col >= 0; --col) {
if (input[row][col] > highest) {
output[row][col] = 1;
highest = input[row][col];
}
}
}
for (int col = 0; col < 150; ++col) {
int highest = -1;
for (int row = 0; row < 150; ++row) {
if (input[row][col] > highest) {
output[row][col] = 1;
highest = input[row][col];
}
}
}
for (int col = 0; col < 150; ++col) {
int highest = -1;
for (int row = 149; row >= 0; --row) {
if (input[row][col] > highest) {
output[row][col] = 1;
highest = input[row][col];
}
}
}
int visNum = 0;
for (int i = 0; i < 150; ++i) {
for (int j = 0; j < 150; ++j) {
if (output[i][j] == 1) {
++visNum;
}
}
}
return visNum;
}
}