-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day5a.java
174 lines (147 loc) · 4.56 KB
/
Day5a.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package aoc22;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.EmptyStackException;
import java.util.List;
import java.util.Stack;
public class Day5a {
private static String filename = "./src/aoc22/input5.txt";
// private static String filename = "./src/aoc22/example5.txt";
private Stack<String> stack1 = new Stack<>();
private Stack<String> stack2 = new Stack<>();
private Stack<String> stack3 = new Stack<>();
private Stack<String> stack4 = new Stack<>();
private Stack<String> stack5 = new Stack<>();
private Stack<String> stack6 = new Stack<>();
private Stack<String> stack7 = new Stack<>();
private Stack<String> stack8 = new Stack<>();
private Stack<String> stack9 = new Stack<>();
public Day5a(List<String> input) {
// example();
real();
parse(input);
System.out.println("Top boxes: " + peek(stack1) + peek(stack2) + peek(stack3) + peek(stack4) + peek(stack5)
+ peek(stack6) + peek(stack7) + peek(stack8) + peek(stack9));
}
private void example() {
stack1.push("Z");
stack1.push("N");
stack2.push("M");
stack2.push("C");
stack2.push("D");
stack3.push("P");
}
private void real() {
stack1.push("W");
stack1.push("B");
stack1.push("D");
stack1.push("N");
stack1.push("C");
stack1.push("F");
stack1.push("J");
stack2.push("P");
stack2.push("Z");
stack2.push("V");
stack2.push("Q");
stack2.push("L");
stack2.push("S");
stack2.push("T");
stack3.push("P");
stack3.push("Z");
stack3.push("B");
stack3.push("G");
stack3.push("J");
stack3.push("T");
stack4.push("D");
stack4.push("T");
stack4.push("L");
stack4.push("J");
stack4.push("Z");
stack4.push("B");
stack4.push("H");
stack4.push("C");
stack5.push("G");
stack5.push("V");
stack5.push("B");
stack5.push("J");
stack5.push("S");
stack6.push("P");
stack6.push("S");
stack6.push("Q");
stack7.push("B");
stack7.push("V");
stack7.push("D");
stack7.push("F");
stack7.push("L");
stack7.push("M");
stack7.push("P");
stack7.push("N");
stack8.push("P");
stack8.push("S");
stack8.push("M");
stack8.push("F");
stack8.push("B");
stack8.push("D");
stack8.push("L");
stack8.push("R");
stack9.push("V");
stack9.push("D");
stack9.push("T");
stack9.push("R");
}
private String peek(Stack<String> stack) {
try {
return stack.peek();
} catch (EmptyStackException e) {
return "";
}
}
private void parse(List<String> input) {
for (String line : input) {
int amount = readAmount(line);
int fromStackNumber = readFromStack(amount, line);
int toStackNumber = readToStack(amount, line);
Stack<String> fromStack = toStack(fromStackNumber);
Stack<String> toStack = toStack(toStackNumber);
for (int i = 0; i < amount; i++) {
toStack.push(fromStack.pop());
}
}
}
private int readAmount(String line) {
if (line.charAt(6) == ' ') {
return Integer.parseInt("" + line.charAt(5));
}
return Integer.parseInt("" + line.charAt(5) + line.charAt(6));
}
private int readFromStack(int amount, String line) {
if (amount >= 10) {
return Integer.parseInt("" + line.charAt(13));
}
return Integer.parseInt("" + line.charAt(12));
}
private int readToStack(int amount, String line) {
if (amount >= 10) {
return Integer.parseInt("" + line.charAt(18));
}
return Integer.parseInt("" + line.charAt(17));
}
private Stack<String> toStack(int stackNumber) {
return switch (stackNumber) {
case 1 -> stack1;
case 2 -> stack2;
case 3 -> stack3;
case 4 -> stack4;
case 5 -> stack5;
case 6 -> stack6;
case 7 -> stack7;
case 8 -> stack8;
case 9 -> stack9;
default -> throw new IllegalArgumentException("Unexpected value: " + stackNumber);
};
}
public static void main(String[] args) throws IOException {
new Day5a(Files.readAllLines(Path.of(filename)));
}
}