-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day18.java
285 lines (270 loc) · 7.46 KB
/
Day18.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
class TreePairNode{
public boolean isLeftChild;
public TreePairNode LEFT;
public TreePairNode RIGHT;
public boolean literalLeft;
public boolean literalRight;
public TreePairNode parent;
public int leftValue;
public int rightValue;
public int depth;
public long getMagnitude() {
return 3*getLeftMagnitude() + 2*getRightMagnitude();
}
public long getLeftMagnitude() {
return (literalLeft) ? leftValue : LEFT.getMagnitude();
}
public long getRightMagnitude() {
return (literalRight) ? rightValue : RIGHT.getMagnitude();
}
private TreePairNode splitValue(int value) {
TreePairNode np=new TreePairNode();
np.depth=depth+1;
np.literalLeft=true;
np.literalRight=true;
np.parent=this;
np.leftValue=(int)Math.floor(value/2.0);
np.rightValue=(int)Math.ceil(value/2.0);
return np;
}
public void splitLeft() {
LEFT=splitValue(leftValue);
LEFT.isLeftChild=true;
literalLeft=false;
}
public void splitRight() {
RIGHT=splitValue(rightValue);
RIGHT.isLeftChild=false;
literalRight=false;
}
public boolean addLeftMost(int add,TreePairNode node) { //need leftmost element
if (node==null)
return false;
if (node.literalLeft)
node.leftValue+=add;
else {
TreePairNode onLeft=node.LEFT;
while (true) {
if (onLeft.literalRight) {
onLeft.rightValue+=add;
break;
}
onLeft=onLeft.RIGHT;
}
}
return true;
}
public boolean addRightMost(int add,TreePairNode node) { //need rightmost
if (node==null)
return false;
if (node.literalRight)
node.rightValue+=add;
else
{
TreePairNode onRight=node.RIGHT;
while (true) {
if (onRight.literalLeft) {
onRight.leftValue+=add;
break;
}
onRight=onRight.LEFT;
}
}
return true;
}
public void explodeLeftChild() {
int addOnLeft=LEFT.leftValue;
int addOnRight=LEFT.rightValue;
LEFT=null;
literalLeft=true;
leftValue=0;
if (literalRight)
rightValue+=addOnRight;
else
addLeftMost(addOnRight,RIGHT);
TreePairNode curr=parent;
boolean currLeftIsAcceptable=!isLeftChild;
while (curr!=null) {
if (currLeftIsAcceptable) {
addLeftMost(addOnLeft,curr);
break;
}else {
currLeftIsAcceptable=!curr.isLeftChild;
curr=curr.parent;
}
}
}
public void explodeRightChild() {
int addOnLeft=RIGHT.leftValue;
int addOnRight=RIGHT.rightValue;
RIGHT=null;
literalRight=true;
rightValue=0;
if (literalLeft)
leftValue+=addOnLeft;
else
addRightMost(addOnLeft,LEFT);
TreePairNode curr=parent;
boolean currRightIsAcceptable=isLeftChild;
while (curr!=null) {
if (currRightIsAcceptable) {
addRightMost(addOnRight,curr);
break;
}else {
currRightIsAcceptable=curr.isLeftChild;
curr=curr.parent;
}
}
}
}
public class Day18 {
public static void parseString(String str,TreePairNode curr,int charposition) {
if (str.charAt(charposition)=='[') {
if (str.charAt(charposition+1)!='['){
curr.literalLeft=true;
curr.leftValue=Integer.parseInt(Character.toString(str.charAt(charposition+1)));
parseString(str,curr,charposition+2);
}else {
TreePairNode createLeft=new TreePairNode();
createLeft.parent=curr;
createLeft.isLeftChild=true;
curr.literalLeft=false;
createLeft.depth=curr.depth+1;
parseString(str,createLeft,charposition+1);
curr.LEFT=createLeft;
}
}else if (str.charAt(charposition)==',') {
if (str.charAt(charposition+1)!='['){
curr.literalRight=true;
curr.rightValue=Integer.parseInt(Character.toString(str.charAt(charposition+1)));
parseString(str,curr,charposition+2);
}else {
TreePairNode createRight=new TreePairNode();
createRight.parent=curr;
createRight.isLeftChild=false;
createRight.depth=curr.depth+1;
curr.literalRight=false;
parseString(str,createRight,charposition+1);
curr.RIGHT=createRight;
}
}else {
if (charposition+1!=str.length())
parseString(str,curr.parent,charposition+1);
}
}
public static void recIncreaseDepthProcedure(TreePairNode curr) {
curr.depth++;
if (!curr.literalLeft)
recIncreaseDepthProcedure(curr.LEFT);
if (!curr.literalRight)
recIncreaseDepthProcedure(curr.RIGHT);
}
public static TreePairNode joinTwoTrees(TreePairNode pair1,TreePairNode pair2) {
TreePairNode newRoot=new TreePairNode();
newRoot.literalLeft=false;
newRoot.literalRight=false;
newRoot.LEFT=pair1;
pair1.parent=newRoot;
pair2.parent=newRoot;
pair1.isLeftChild=true;
pair2.isLeftChild=false;
newRoot.RIGHT=pair2;
newRoot.depth=1;
recIncreaseDepthProcedure(pair1);
recIncreaseDepthProcedure(pair2);
return newRoot;
}
public static int recExplode(TreePairNode curr) {
int changes=0;
if (curr!=null) {
if (!curr.literalLeft)
if (curr.LEFT.literalLeft && curr.LEFT.literalRight && curr.LEFT.depth>=5) {
curr.explodeLeftChild();
changes++;
}
changes+=recExplode(curr.LEFT);
if (!curr.literalRight)
if (curr.RIGHT.literalLeft && curr.RIGHT.literalRight && curr.RIGHT.depth>=5) {
curr.explodeRightChild();
changes++;
}
changes+=recExplode(curr.RIGHT);
}
return changes;
}
public static int recSplit(TreePairNode curr) {
int changes=0;
if (curr!=null) {
if (curr.literalLeft && curr.leftValue>9) {
curr.splitLeft();
return 1;
}
changes=recSplit(curr.LEFT);
if (changes>0)
return 1;
if (curr.literalRight && curr.rightValue>9) {
curr.splitRight();
return 1;
}
changes+=recSplit(curr.RIGHT);
}
return changes;
}
public static void reduce(TreePairNode head) {
int changes=1;
while (changes>0)
changes=recExplode(head) + recSplit(head);
}
public static long function(String fname) throws IOException {
BufferedReader brdr=new BufferedReader(new FileReader(fname));
String ln=brdr.readLine();
TreePairNode root=new TreePairNode(); root.depth=1;
parseString(ln,root,0);
while ((ln=brdr.readLine())!=null) {
TreePairNode root2=new TreePairNode(); root2.depth=1;
parseString(ln,root2,0);
root=joinTwoTrees(root,root2);
reduce(root);
}
return root.getMagnitude();
}
public static long function2(String fname) throws IOException {
BufferedReader brdr=new BufferedReader(new FileReader(fname));
String ln="";
ArrayList<String> roots=new ArrayList<String>();
while ((ln=brdr.readLine())!=null) {
TreePairNode root=new TreePairNode(); root.depth=1;
parseString(ln,root,0);
roots.add(ln);
}
long maxMagnitute=0;
for (int i=0;i<roots.size();++i) {
for (int j=0;j<roots.size();++j) {
if (i!=j) {
TreePairNode root1=new TreePairNode(); root1.depth=1;
parseString(roots.get(i),root1,0);
TreePairNode root2=new TreePairNode(); root2.depth=1;
parseString(roots.get(j),root2,0);
TreePairNode root12=joinTwoTrees(root1,root2);
reduce(root12);
maxMagnitute=Math.max(maxMagnitute,root12.getMagnitude());
}
}
}
return maxMagnitute;
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String SAMPLE="input/day18_sample.txt";
String REAL="input/day18.txt";
System.out.println(function(SAMPLE));
System.out.println(function(REAL));
System.out.println(function2(SAMPLE));
System.out.println(function2(REAL));
}
}