-
Notifications
You must be signed in to change notification settings - Fork 17
/
Day8.java
67 lines (58 loc) · 1.58 KB
/
Day8.java
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
package com.sbaars.adventofcode.year22.days;
import com.sbaars.adventofcode.common.Direction;
import com.sbaars.adventofcode.common.grid.NumGrid;
import com.sbaars.adventofcode.year22.Day2022;
import java.awt.*;
import java.io.IOException;
public class Day8 extends Day2022 {
public Day8() {
super(8);
}
public static void main(String[] args) throws IOException {
new Day8().printParts();
}
@Override
public Object part1() {
NumGrid grid = new NumGrid(day(), "\n", "");
return grid.stream().filter(p -> findEdge(grid, p)).count();
}
private boolean findEdge(NumGrid grid, Point p) {
Direction[] dirs = Direction.fourDirections();
long num = grid.get(p);
for (Direction d : dirs) {
Point newLoc = p;
while (true) {
newLoc = d.move(newLoc);
long atLoc = grid.get(newLoc);
if (atLoc >= num) break;
else if (atLoc == -1) return true;
}
}
return false;
}
@Override
public Object part2() {
NumGrid grid = new NumGrid(day(), "\n", "");
return grid.stream().mapToLong(p -> scenicScore(grid, p)).max().getAsLong();
}
private long scenicScore(NumGrid grid, Point p) {
Direction[] dirs = Direction.fourDirections();
long num = grid.get(p);
long score = 1;
for (Direction d : dirs) {
Point newLoc = p;
long s = 0;
while (true) {
newLoc = d.move(newLoc);
long atLoc = grid.get(newLoc);
if (atLoc >= num) {
s++;
break;
} else if (atLoc == -1) break;
s++;
}
score *= s;
}
return score;
}
}