-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay12a.java
More file actions
162 lines (148 loc) · 3.42 KB
/
Day12a.java
File metadata and controls
162 lines (148 loc) · 3.42 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
import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.util.LinkedList;
import java.util.Queue;
public class Day12a {
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/Day12input.txt");
Scanner scanner = myScanner(text);
if (scanner == null) {
return;
}
int ROWS = 41;
int COLS = 114;
Square[][] heightMap = new Square[ROWS][COLS];
Square startSquare = null;
String line;
int row = 0;
while ((line = myRead(scanner)) != null) {
int col = 0;
for (int i = 0; i < line.length(); ++i) {
char c = line.charAt(i);
if (c == 'S') {
startSquare = new Square(row, col, 0, false);
heightMap[row][col] = startSquare;
} else if (c == 'E') {
heightMap[row][col] = new Square(row, col, 26, true);
} else if (c != '\n') {
heightMap[row][col] = new Square(row, col, c - 'a' + 1, false);
}
++col;
}
++row;
}
Queue<Square> q = new LinkedList<>();
startSquare.isPassed();
q.add(startSquare);
int steps = 0;
int counter = 1;
int nextCounter;
while (counter > 0) {
Square tmp;
nextCounter = 0;
while (counter > 0) {
tmp = q.poll();
if (tmp.isEnd()) {
System.out.println("Steps: " + steps);
return;
}
nextCounter += tmp.addNewAdj(q, heightMap, ROWS, COLS);
--counter;
}
counter = nextCounter;
steps++;
}
System.out.println("no way to End");
}
}
class Square {
int row;
int col;
int height;
boolean passed;
boolean isEnd;
public Square(int x, int y, int height, boolean isEnd) {
this.row = x;
this.col = y;
this.height = height;
this.passed = false;
this.isEnd = isEnd;
}
public int addNewAdj(Queue<Square> q, Square[][] heightMap, int rows, int cols) {
Square tmp;
int counter = 0;
if (row - 1 >= 0) {
tmp = heightMap[row - 1][col];
if (!tmp.passed && this.isClimable(tmp)) {
tmp.isPassed();
q.add(tmp);
++counter;
}
}
if (col - 1 >= 0) {
tmp = heightMap[row][col - 1];
if (!tmp.passed && this.isClimable(tmp)) {
tmp.isPassed();
q.add(tmp);
++counter;
}
}
if (row + 1 < rows) {
tmp = heightMap[row + 1][col];
if (!tmp.passed && this.isClimable(tmp)) {
tmp.isPassed();
q.add(tmp);
++counter;
}
}
if (col + 1 < cols) {
tmp = heightMap[row][col + 1];
if (!tmp.passed && this.isClimable(tmp)) {
tmp.isPassed();
q.add(tmp);
++counter;
}
}
return counter;
}
private boolean isClimable(Square next) {
int heightDiff = next.height - this.height;
if (heightDiff <= 1) {
return true;
}
return false;
}
public boolean isEnd() {
return this.isEnd;
}
public void isPassed() {
this.passed = true;
}
public int getHeight() {
return this.height;
}
@Override
public String toString() {
return Integer.toString(this.height);
}
}