-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay12.java
More file actions
80 lines (76 loc) · 3.24 KB
/
Day12.java
File metadata and controls
80 lines (76 loc) · 3.24 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
package org.thehuglio;
import java.io.File;
import java.util.*;
public class Main {
private static final Reader reader = new Reader(new File("data.txt"));
public static int x = reader.data.get(1).toCharArray().length;
public static int y = reader.data.size();
public static int[][] hgrid = new int[x][y];
public static int[][] sgrid = new int[x][y];
public static void main(String[] args) {
int endx = 0;
int endy = 0;
System.out.println(reader.data);
for (int i = 0; i < y; i++) {
for (int j = 0; j < x; j++) {
if (reader.data.get(i).toCharArray()[j] == 'a') { // change 'a' to 'S' for part 1
hgrid[j][i] = 1;
sgrid[j][i] = 1;
}else if (reader.data.get(i).toCharArray()[j] == 'E') {
hgrid[j][i] = 26;
endx = j;
endy = i;
} else {
String index = reader.data.get(i).split("")[j];
String[] c = "abcdefghijklmnopqrstuvwxyz".split("");
hgrid[j][i] = Arrays.asList(c).indexOf(index) + 1;
}
}
}
int check = 0;
while (check<100) {
for (int y = 0; y < Main.y; y++) {
for (int x = 0; x < Main.x; x++) {
if (x != 0) {
if ((sgrid[x - 1][y] < sgrid[x][y] || sgrid[x][y] == 0) && hgrid[x - 1][y] + 1 >= hgrid[x][y] && sgrid[x - 1][y] != 0) {
sgrid[x][y] = sgrid[x - 1][y] + 1;
}
}
if (x != Main.x - 1) {
if ((sgrid[x + 1][y] < sgrid[x][y] || sgrid[x][y] == 0) && hgrid[x + 1][y] + 1 >= hgrid[x][y] && sgrid[x + 1][y] != 0) {
sgrid[x][y] = sgrid[x + 1][y] + 1;
}
}
if (y != 0) {
if ((sgrid[x][y - 1] < sgrid[x][y] || sgrid[x][y] == 0) && hgrid[x][y - 1] + 1 >= hgrid[x][y] && sgrid[x][y - 1] != 0) {
sgrid[x][y] = sgrid[x][y - 1] + 1;
}
}
if (y != Main.y - 1) {
if ((sgrid[x][y + 1] < sgrid[x][y] || sgrid[x][y] == 0) && hgrid[x][y + 1] + 1 >= hgrid[x][y] && sgrid[x][y + 1] != 0) {
sgrid[x][y] = sgrid[x][y + 1] + 1;
}
}
}
}
for (int y = 0; y < Main.y; y++) {
StringBuilder stringBuilder = new StringBuilder();
for (int x = 0; x < Main.x; x++) {
if (sgrid[x][y] < 10) {
stringBuilder.append(0);
}
if (sgrid[x][y] < 100) {
stringBuilder.append(0);
}
stringBuilder.append(sgrid[x][y]);
stringBuilder.append(" ");
}
System.out.println(stringBuilder);
}
if (sgrid[endx][endy] != 0) {
System.out.println(sgrid[endx][endy] - 1);
check++;
}
}
}
}