-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay10.java
142 lines (133 loc) · 4.89 KB
/
Day10.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
package src.solutions;
import src.meta.DayTemplate;
import src.objects.Coordinate;
import java.util.*;
public class Day10 implements DayTemplate {
List<List<String>> grid;
int[][] distances;
Coordinate prev;
Set<Coordinate> been;
@Override
public String[] fullSolve(Scanner in) {
long answer1 = 0;
long answer2 = 0;
parse(in);
//next five lines are a manual step, since S is unknown.
been.add(prev);
assert prev != null;
Coordinate curr = new Coordinate(prev.x, prev.y + 1);
distances[curr.x * 2 + 1][curr.y * 2 + 1] = 1;
distances[curr.x + prev.x + 1][curr.y + prev.y + 1] = 1;
answer1++;
answer1 += traverseGrid(curr);
answer1 /= 2;
floodFill();
for (int i = 1; i < distances.length; i += 2) {
for (int j = 1; j < distances[i].length; j += 2) {
if (distances[i][j] == 2) {
answer2 += 1;
}
}
}
return new String[]{answer1 + "", answer2 + ""};
}
/**
* Main solving method.
*
* @param part1 The solver will solve part 1 if param is set to true.
* The solver will solve part 2 if param is set to false.
* @param in The solver will read data from this Scanner.
* @return Returns answer in string format.
*/
public String solve(boolean part1, Scanner in) {
long answer = 0;
parse(in);
//next five lines are a manual step, since S is unknown.
been.add(prev);
assert prev != null;
Coordinate curr = new Coordinate(prev.x, prev.y + 1);
distances[curr.x * 2 + 1][curr.y * 2 + 1] = 1;
distances[curr.x + prev.x + 1][curr.y + prev.y + 1] = 1;
answer++;
answer += traverseGrid(curr);
if (!part1) {
floodFill();
answer = 0;
for (int i = 1; i < distances.length; i += 2) {
for (int j = 1; j < distances[i].length; j += 2) {
if (distances[i][j] == 2) {
answer += 1;
}
}
}
}
if (part1) {
answer /= 2;
}
return answer + "";
}
private void floodFill() {
List<Coordinate> pointsToCheck = new ArrayList<>();
pointsToCheck.add(new Coordinate(0, 0));
int[] xs = new int[]{-1, 1, 0, 0};
int[] ys = new int[]{0, 0, -1, 1};
while (!pointsToCheck.isEmpty()) {
List<Coordinate> tmp = new ArrayList<>();
for (Coordinate point : pointsToCheck) {
for (int k = 0; k < 4; k++) {
int newx = point.x + xs[k];
int newy = point.y + ys[k];
if (newx >= 0 && newy >= 0 && newx < distances.length && newy < distances[0].length && distances[newx][newy] == 2) {
distances[newx][newy] = 0;
tmp.add(new Coordinate(newx, newy));
}
}
pointsToCheck = tmp;
}
}
}
private int traverseGrid(Coordinate curr) {
Map<String, Integer> types = Map.of("|", 10102, "-", 12010, "F", 11001, "J", 10220, "7", 12001, "L", 10210);
int answer = 0;
while (!been.contains(curr)) {
Coordinate tmp = curr;
answer += 1;
Integer pipe = types.get(grid.get(curr.y).get(curr.x));
int x1 = (pipe / 1000 % 10) == 2 ? -1 : (pipe / 1000 % 10);
int y1 = (pipe / 100 % 10) == 2 ? -1 : (pipe / 100 % 10);
int x2 = (pipe / 10 % 10) == 2 ? -1 : (pipe / 10 % 10);
int y2 = (pipe % 10) == 2 ? -1 : (pipe % 10);
Coordinate case1 = new Coordinate(curr.x + x1, curr.y + y1);
if (prev.x == case1.x && prev.y == case1.y) {
curr = new Coordinate(curr.x + x2, curr.y + y2);
} else {
curr = case1;
}
prev = tmp;
been.add(prev);
distances[curr.x * 2 + 1][curr.y * 2 + 1] = 1;
distances[curr.x + prev.x + 1][curr.y + prev.y + 1] = 1;
}
answer++;
return answer;
}
private void parse(Scanner in) {
grid = new ArrayList<>();
while (in.hasNext()) {
String line = in.nextLine();
grid.add(Arrays.stream(line.split("")).toList());
}
distances = new int[grid.size() * 2 + 1][grid.get(0).size() * 2 + 1];
prev = null;
been = new HashSet<>();
for (int i = 0; i < distances.length; i++) {
for (int j = 0; j < distances[0].length; j++) {
if (i % 2 == 1 && j % 2 == 1 && grid.get(i / 2).get(j / 2).equals("S")) {
prev = new Coordinate(j / 2, i / 2);
} else {
distances[i][j] = 2;
}
}
}
}
}