-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC5.java
93 lines (73 loc) · 2.83 KB
/
AoC5.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
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
public class AoC5 {
static class Movement {
int cratesN;
int fromStack;
int toStack;
public Movement(int cratesN, int fromStack, int toStack) {
this.cratesN = cratesN;
this.fromStack = fromStack;
this.toStack = toStack;
}
@Override
public String toString() {
return cratesN + " - " + fromStack + " - " + toStack;
}
}
public static void main(String[] args) {
String fileName = "AoC5";
try {
var file = Arrays.stream(Files.readString(Paths.get(fileName))
.split("\n"))
.toList();
// parser
List<Deque<String>> stacks = new ArrayList<>();
List<Movement> movements = new ArrayList<>();
boolean isMovements = false;
for (String s : file) {
if (!isMovements) {
if (!s.isEmpty()) {
String crates = s.replace(" ","[x]").replace(" ", "").replace("[","").replace("]","");
if (!Character.isDigit(crates.charAt(0))) {
for (int i = 0; i < crates.length(); i++) {
if (stacks.size() <= i) {
stacks.add(new ArrayDeque<>());
}
if (crates.charAt(i) != 'x') {
stacks.get(i).addLast(String.valueOf(crates.charAt(i)));
}
}
}
} else {
isMovements = true;
}
} else {
String[] m = s.replace("move ", "").replace(" from ", ",").replace(" to ", ",").split(",");
Movement movement = new Movement(
Integer.parseInt(m[0]),
Integer.parseInt(m[1]),
Integer.parseInt(m[2])
);
movements.add(movement);
}
}
for (Movement movement : movements) {
System.out.println(movement);
for (int i = 0; i < movement.cratesN; i++) {
stacks.get(movement.toStack - 1).push(stacks.get(movement.fromStack - 1).pop());
}
System.out.println(stacks);
}
String endResult = "";
for (Deque<String> stack : stacks) {
endResult += stack.getFirst();
}
System.out.println(endResult);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}