-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay5.java
More file actions
67 lines (61 loc) · 1.88 KB
/
Day5.java
File metadata and controls
67 lines (61 loc) · 1.88 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
import java.util.ArrayList;
import java.util.Stack;
public class Day5 {
public static void main(String[] args) {
new Day5();
}
public Day5() {
ArrayList<String> input = ReadInput.read("res/input5.txt");
partOne(input);
partTwo(input);
}
private void partOne(ArrayList<String> input) {
ArrayList<Stack<Character>> stacks = initStacks();
for(String instruction : input) {
String[] splitRow = instruction.split(" ");
int amount = Integer.parseInt(splitRow[1]);
int from = Integer.parseInt(splitRow[3]) - 1;
int to = Integer.parseInt(splitRow[5]) - 1;
for(int i = 0; i < amount; i++) {
stacks.get(to).push(stacks.get(from).pop());
}
}
StringBuilder res = new StringBuilder();
for(Stack<Character> s : stacks) {
res.append(s.peek());
}
System.out.println(res.toString());
}
private void partTwo(ArrayList<String> input) {
ArrayList<Stack<Character>> stacks = initStacks();
for(String instruction : input) {
String[] splitRow = instruction.split(" ");
int amount = Integer.parseInt(splitRow[1]);
int from = Integer.parseInt(splitRow[3]) - 1;
int to = Integer.parseInt(splitRow[5]) - 1;
Stack<Character> tmpStack = new Stack<Character>();
for(int i = 0; i < amount; i++) {
tmpStack.push(stacks.get(from).pop());
}
for(int i = 0; i < amount; i++) {
stacks.get(to).push(tmpStack.pop());
}
}
StringBuilder res = new StringBuilder();
for(Stack<Character> s : stacks) {
res.append(s.peek());
}
System.out.println(res.toString());
}
private ArrayList<Stack<Character>> initStacks() {
String input = "QWPSZRHD VBRWQHF CVSH HFG PGJBZ QTJHWFL ZTWDLVJN DTZCJGHF WPVMBH";
ArrayList<Stack<Character>> stacks = new ArrayList<Stack<Character>>();
for(String s : input.split(" ")) {
stacks.add(new Stack<Character>());
for(char c : s.toCharArray()) {
stacks.get(stacks.size() - 1).push(c);
}
}
return stacks;
}
}