-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDay9.java
152 lines (120 loc) · 3.91 KB
/
Day9.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
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
package aoc21;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.stream.Collectors;
import utils.Point2d;
public class Day9 {
private Map<Point2d, Integer> heightMap;
public Day9(File file) {
heightMap = initHeightMap(file);
}
private int run1() {
return heightMap
.entrySet()
.stream()
.filter(p -> isLowPoint(p.getKey()))
.mapToInt(p -> heightMap.get(p.getKey()) + 1)
.sum();
}
private boolean isLowPoint(Point2d point) {
Point2d n = new Point2d(point.x(), point.y() - 1);
Point2d s = new Point2d(point.x(), point.y() + 1);
Point2d w = new Point2d(point.x() - 1, point.y());
Point2d e = new Point2d(point.x() + 1, point.y());
int h = heightMap.get(point);
return h < heightMap.getOrDefault(n, 10)
&& h < heightMap.getOrDefault(s, 10)
&& h < heightMap.getOrDefault(w, 10)
&& h < heightMap.getOrDefault(e, 10);
}
private int run2() {
List<Point2d> lowPoints = heightMap
.entrySet()
.stream()
.filter(e -> isLowPoint(e.getKey()))
.map(e -> e.getKey())
.collect(Collectors.toList());
List<List<Integer>> basins = new ArrayList<>();
for(Point2d lowPoint : lowPoints) {
List<Integer> basin = new ArrayList<>();
Queue<Point2d> pointQueue = new LinkedList<>();
pointQueue.add(lowPoint);
// flood fill
Set<Point2d> visited = new HashSet<>();
visited.add(lowPoint);
while(!pointQueue.isEmpty()) {
Point2d currentPoint = pointQueue.poll();
basin.add(heightMap.get(currentPoint));
Point2d n = new Point2d(currentPoint.x(), currentPoint.y() - 1);
Point2d s = new Point2d(currentPoint.x(), currentPoint.y() + 1);
Point2d w = new Point2d(currentPoint.x() - 1, currentPoint.y());
Point2d e = new Point2d(currentPoint.x() + 1, currentPoint.y());
if(heightMap.containsKey(n) && !visited.contains(n) && heightMap.get(n) < 9) {
pointQueue.add(n);
visited.add(n);
}
if(heightMap.containsKey(s) && !visited.contains(s) && heightMap.get(s) < 9) {
pointQueue.add(s);
visited.add(s);
}
if(heightMap.containsKey(w) && !visited.contains(w) && heightMap.get(w) < 9) {
pointQueue.add(w);
visited.add(w);
}
if(heightMap.containsKey(e) && !visited.contains(e) && heightMap.get(e) < 9) {
pointQueue.add(e);
visited.add(e);
}
}
basins.add(basin);
}
basins.sort((l1, l2) -> Integer.compare(l2.size(), l1.size()));
return basins.stream().limit(3).mapToInt(l -> l.size()).reduce(1, (a, b) -> a * b);
}
private Map<Point2d, Integer> initHeightMap(File input) {
Map<Point2d, Integer> heightMap = new HashMap<>();
try {
BufferedReader br = new BufferedReader(new FileReader(input));
String line = "";
int row = 0;
int col = 0;
while ((line = br.readLine()) != null) {
for(int i = 0; i < line.length(); i++) {
heightMap.put(new Point2d(col++, row), Integer.parseInt(Character.toString(line.charAt(i))));
}
col = 0;
row++;
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return heightMap;
}
@SuppressWarnings("unused")
private void printMap() {
int xMax = heightMap.entrySet().stream().mapToInt(h -> h.getKey().x()).max().getAsInt();
int yMax = heightMap.entrySet().stream().mapToInt(h -> h.getKey().y()).max().getAsInt();
for(int i = 0; i <= yMax; i++) {
for(int j = 0; j <= xMax; j++) {
System.out.print(heightMap.get(new Point2d(j, i)));
}
System.out.println();
}
}
public static void main(String[] args) {
Day9 test = new Day9(new File("C:\\Users\\tmerdin\\aoc\\aoc21\\day9\\InputFile1.txt"));
System.out.println(test.run1());
System.out.println(test.run2());
}
}