-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay5.java
More file actions
133 lines (111 loc) · 2.48 KB
/
Day5.java
File metadata and controls
133 lines (111 loc) · 2.48 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
import java.io.*;
public class Day5 {
public static void main(String[] args) {
Console cnsl = System.console();
Stack[] stacks = new Stack[9];
for (int i = 0; i < 9; ++i) {
stacks[i] = new Stack();
}
String start = cnsl.readLine();
while (start.length() > 2) {
for (int i = 0; i < (start.length() / 4) + 1; ++i) {
if (start.charAt(i*4) == '[') {
Box tmp = new Box(start.charAt(i*4 + 1));
stacks[i].add(tmp);
}
}
start = cnsl.readLine();
}
for (int i = 0; i < 9; ++i) {
stacks[i].reverse();
}
String command = cnsl.readLine();
int num, from, to;
num = from = to = 0;
while (command.length() > 2) {
String[] input = command.split(" ", 0);
num = Integer.valueOf(input[1]);
from = Integer.valueOf(input[3]);
to = Integer.valueOf(input[5]);
// Solution from part 1
/*
for (int i = 0; i < num; ++i) {
stacks[to - 1].add(stacks[from - 1].remove());
}
*/
// Solution for part 2
Stack tmp = new Stack();
for (int i = 0; i < num; ++i) {
tmp.add(stacks[from - 1].remove());
}
tmp.reverse();
stacks[to - 1].add(tmp);
command = cnsl.readLine();
}
for (int i = 0; i < 9; ++i) {
System.out.println("stack " + (i+1) + " starting from top: " + stacks[i]);
}
}
}
class Box {
char letter;
Box under;
public Box(char x) {
this.letter = x;
this.under = null;
}
public void over(Box x) {
this.under = x;
}
public Box under() {
return this.under;
}
public String toString() {
return String.valueOf(this.letter);
}
}
class Stack {
Box top;
public Stack() {
this.top = null;
}
public void add(Box x) {
x.over(this.top);
this.top = x;
}
public void add(Stack x) {
Box head = x.top;
Box curr = head;
while (curr.under() != null) {
curr = curr.under();
}
curr.over(this.top);
this.top = head;
}
public Box remove() {
Box res = this.top;
this.top = res.under();
return res;
}
public void reverse() {
Box prev = null;
Box curr = this.top;
Box next = null;
while (curr != null) {
next = curr.under();
curr.over(prev);
prev = curr;
curr = next;
}
this.top = prev;
}
public String toString() {
String res = "";
Box tmp = this.top;
while (tmp != null) {
res += tmp.toString();
tmp = tmp.under();
}
return res;
}
}